问题
I'm new to SQL, and I'm trying to learn it. I'm creating a procedure to INSERT Members into Member table.
Im using this (which ofc works):
CREATE PROCEDURE InsertMember
AS
INSERT INTO Member (FirstName, LastName, City)
VALUES ('Blla', 'Blla', 'Blla')
But... I want to know if it is possible to create a procedure which when I execute it, to ask user for input. For example: When I execute the procedure it should go like this:
@FirstName = 'SOME FIRSTNAME'
@LastName = 'SOME LASTNAME'
@City = 'SOME CITY'
I didn't try anything yet, because I don't know if that is possible. Help please ? Any reference ?
回答1:
Try this, change your stored procedure to accept parameters
CREATE PROCEDURE InsertMember
(@FirstName varchar(50), @LastName varchar(50), @City varchar(50))
AS
BEGIN
INSERT INTO Member (FirstName, LastName, City)
VALUES (@FirstName, @LastName, @City)
END
Now you can call this SP in SQL Management Studio
exec InsertMember 'Blla','Blla','Blla'
回答2:
Alter the procedure:
ALTER PROC SP_GETCUSTOMER
(@CUSTOMERID NCHAR,@CITY NVARCHAR(50),
@COUNTRY NVARCHAR(50))
AS
BEGIN
SELECT ContactName,CompanyName FROM DimCustomers
END
来源:https://stackoverflow.com/questions/16124557/creating-procedure-sql-server-2008-r2