我需要确认尚未输入客户,在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
找到答案@ http://www.sqlservertutorial.net/sql-server-stored-procedures/variables/
SET @your_variable =(选择计数(*)从yourtable.yourcolumn);