Creating procedure SQL Server 2008 R2

坚强是说给别人听的谎言 提交于 2020-01-14 01:40:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!