开发者社区> 问答> 正文

MySQL vs SQLSERVER语法,用于选择声明的变量

我需要验证尚未输入客户,在mysql中,我可以通过在下面的变量(@V_CustomerID)中选择客户ID来做到这一点。但是,ssms不喜欢该语法,还有另一种方法可以实现此目的吗?

Create procedure AddCustomer(
    @CustomerFirstName as varchar(50),
    @CustomerLastName as varchar(50),
    @SiteName as varchar(50),
    @CustomerPhone1 as varchar(20),
    @CustomerPhone2 as varchar(20),
    @CustomerAddress1 as varchar(20),
    @CustomerAddress2 as varchar(20),
    @CustomerCity as varchar(50),
    @CustomerState as varchar(5),
    @CustomerZip as char(5)
    )
    as
    begin

        declare @V_CustomerID as int,--use this to store the returned value
                @V_CustomerExists as bit --use this to store the result of the if else condition

        --First we should check if the customer data already exists in the table--
            select dbo.customer.CustomerID 
                into @V_CustomerID
            from 
                dbo.Customer
            where 
                dbo.customer.FirstName = @CustomerFirstName and dbo.customer.LastName = @CustomerLastName and dbo.customer.sitename = @SiteName;

        if @V_CustomerID is null

        begin
                --Yay!  we can insert the customer--
                        insert into Customer
                        values(@CustomerFirstName,@CustomerLastName,@SiteName);
        end
        else
        begin
                set @V_CustomerExists = 1 
                print 'Customer already exists';

        end

    end

展开
收起
心有灵_夕 2019-12-22 14:04:14 785 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像