加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 动态SQL中SQL绑定变量的SQL Server等价物是什么?

发布时间:2020-12-12 08:26:47 所属栏目:MsSql教程 来源:网络整理
导读:在Oracle中,编写动态SQL时,会执行以下操作: create or replace procedure myProc(n in number)asbegin execute immediate 'update myTable set myColumn = :n' using n;commit;end; 然后’魔术发生’. SQL Server中等效的概念/语法是什么(如果有的话)? (顺
在Oracle中,编写动态SQL时,会执行以下操作:
create or replace procedure myProc(n in number)
as
begin
  execute immediate
   'update myTable set myColumn = :n' 
   using n;
commit;
end;

然后’魔术发生’. SQL Server中等效的概念/语法是什么(如果有的话)? (顺便说一句,我正在使用SQL Server 2005)

解决方法

您将使用sp_executesql.绑定变量如下所示:@ var1.

从下面的链接,对标准Northwind数据库的示例查询:

DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);

/* Build the SQL string one time.*/
SET @SQLString =
     N'SELECT BusinessEntityID,NationalIDNumber,JobTitle,LoginID
       FROM AdventureWorks2008R2.HumanResources.Employee 
       WHERE BusinessEntityID = @BusinessEntityID';
SET @ParmDefinition = N'@BusinessEntityID tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @SQLString,@ParmDefinition,@BusinessEntityID = @IntVariable;
/* Execute the same string with the second parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @SQLString,@BusinessEntityID = @IntVariable;

完整详细信息和示例语法位于以下链接:

http://msdn.microsoft.com/en-us/library/ms188001.aspx

http://msdn.microsoft.com/en-us/library/ms175170.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读