use*_*150 1 sql sql-server stored-procedures
SQL Server Management Studio,存储过程
我customerName从Visual Studio 传递,并在存储过程中我想得到customerID.
现在我想检查是否customerId为null,这意味着这是一个新客户.如果是这样,我想在表中插入该客户.然后我想获得客户ID,并希望存储TableB.
这是示例代码:
还告诉如何执行
if(@customerId == null)
{
Insert customerDetails into CustomerTable;
get CustomerId.
} //General workflow what i want.
Run Code Online (Sandbox Code Playgroud)
为此,我编写了一个SQL Server存储过程
create procedure Customers_AddData2
@CustomerName nvarchar(50) = null
As
--Get Customer ID
declare @CustomerID numeric(18, 0)= (SELECT distinct [CustomerID] FROM [tblCustomerMaster] WHERE CustomerName = @CustomerName)
--checking if customerId not found
-- Here is some error, Not getting syntax
IF(@CustomerID IS NULL == true)
{
INSERT INTO [tblCustomerMaster] ([CustomerName])
VALUES (CustomerName)
}
-- Now get customer id
@CustomerID numeric(18, 0)= (SELECT distinct [CustomerID] FROM [tblCustomerMaster] WHERE CustomerName = @CustomerName)
Run Code Online (Sandbox Code Playgroud)
刚刚IF(@CustomerID IS NULL)够
IF(@CustomerID IS NULL)
BEGIN
--insert here..
INSERT INTO [tblCustomerMaster]
([CustomerName]
VALUES
(CustomerName)
SET @CustomerID = Scope_Identity() -- sql server syntax to get the ID after insert..
END
ELSE
BEGIN
-- update perhaps..
END
Run Code Online (Sandbox Code Playgroud)