how to pass variables this in dynamic query in sql

纵饮孤独 提交于 2019-12-02 06:08:35
Jim B

I would use the sp_executesql command.

Some more documentation is here: http://msdn.microsoft.com/en-us/library/ms188001.aspx

Basically, you define a sql query, and parameter list, and then pass those in along with your actual parameters into that method.

So, something like this (real basic)

CREATE PROCEDURE dbo.yourProc
  @customerId INT
AS
DECLARE @sql NVARCHAR(1000)
SET @sql = 'SELECT * FROM Customers WHERE CustomerId = @customerId'

DECLARE @params NVARCHAR(1000)
SET @params = '@customerId INT'

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