欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Pop Rivett‘s SQL Server FAQ
Pop Rivett‘s SQL Server FAQ
18 September 2006

Setting a Variable from Dynamic SQL

Q: Pop Rivett, how do I set variables from dynamically-executed SQL that I can then access?‘

Pop: Bless you, my boy, for asking. Setting a variable within dynamic SQL in Transact SQL is a much more common problem than you might think. From looking at the documentation, you might be led to believe that the only way to pass back results from dynamically-executed SQL is via a result set. But to my way of thinking, where one just wants one or more values in variables, this makes for tedious processing. However, fear not. There is a way of doing it using sp_ExecuteSQL that, for some reason, was never documented in Books Online. Such an omission would never have happened in my day, laddie, I can tell you…

Anyway, I digress. Here is my solution its minimal form. In the list of parameters that you pass to sp_ExecuteSQL, you declare an output variable for values you want passed back to the calling routine, and supply the variable to which to pass it in the parameter value list:

DECLARE @i INT
EXEC
sp_executesql N‘select @i = 999‘, N‘@i int output‘, @i output
SELECT @i

-- You can provide several variables, of course.

DECLARE @i INT, @j INT, @k VARCHAR(20)
EXEC sp_executesql N‘select @i = 34, @j = 644, @k = ‘‘hello world‘‘‘,

  
N‘@i int output,@j int output,@k Varchar(20) output‘, 
  
@i output, @j output, @k output
SELECT @i, @j, @K 

And here is an example of the technique, where one of number of stored procedures which pass back a string output variable is called dynamically. The name of the stored procedure is passed in @SPName and the Dynamic SQL is built up in @SPCall the return value is passed in @rc and any error number is passed into @Error:

DECLARE @OutputParameter VARCHAR(100) ,
       
@error INT ,
       
@SPName VARCHAR(128) ,
       
@SPCall NVARCHAR(128) ,
       
@rc INT
SELECT
@SPCall = ‘exec ‘ + @SPName + ‘ @OutputParameter output‘
EXEC @rc = sp_executesql @SPCall, N‘@OutputParameter varchar(100) output‘,
@OutputParameter output
SELECT @Error = @@error

Q. Gosh Pop, executeSQL seems extraordinarily useful. What other magic does it perform, that isn‘t in BOL?

Pop: Well, let me see, did you know that one can use it to execute dynamic SQL that relies on the database context, in the context of another database / server? No?

Well, it works like this….

EXEC (‘exec svr.dbname.dbo.sp_executesql N‘‘grant select on mytable
to myuser‘‘‘
) 

This will allow myuser to access mytable on the remote server.So. Let‘s imagine you want to write a stored procedure that grants access to a user, @username, on any table, @tablename, in any database, @databasename, based on any server, @servername. You might then use this trick to go about it like this:

DECLARE @sql VARCHAR(1000)
SELECT @sql = ‘exec ‘ + @servername + ‘.‘ + @databasename
+ ‘.dbo.sp_executesql N‘‘grant select on ‘
+ @tablename + ‘ to ‘ + @username + ‘‘‘‘
EXEC (@sql) 

Now run along you young scallywags…and make sure you have execute access to the remote sp_executesql before you try that out!!



This article has been viewed 96 times.

Author profile: Pop Rivett

Pop spent his formative years working in assembler on IBM Series/1 but retrained in VB when that went out of fashion. He soon realised how little relational database expertise existed in most companies and so started to spend most of his time working on that. He now sticks to architecture, release control / IT processes, SQL Server, DTS, SSIS, and access methods in VB/ASP/.NET/Crystal Reports/reporting services. He has been involved with SQL Server since the old days of v4.2 to v2005. He tries to stay away from anything presentation oriented (see www.mindsdoor.net). Theoretically he is semi-retired but seems to keep being offered potentially interesting work.

Search for other articles by Pop Rivett

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
sp_executesql介紹和使用
SQL動(dòng)態(tài)語(yǔ)句用法
學(xué)習SQL應知道的動(dòng)態(tài)SQL語(yǔ)句基本語(yǔ)法
SQL Server 動(dòng)態(tài)SQL
SQL語(yǔ)句臨時(shí)表、表變量注意事項(解決游標性能)
sql sp_executesql 中生成臨時(shí)表的可見(jiàn)性
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久