您的位置:首页 > 运维架构 > Linux

linux特殊符号内置变量

2016-01-18 16:19 477 查看
Unfortunately, in the Microsoft SQL world, sometimes you have to deal
with using OPENQUERY.  This is difficult to use sometimes because you
don't have as much flexibility with variables and parameters as you
would using normal T-SQL.  Below is an example of how you can use
OPENQUERY and still utilize a variable or parameter.

DECLARE @variable VARCHAR(10)

DECLARE @sqlQuery VARCHAR(8000)

DECLARE @finalQuery VARCHAR(8000)

SET @variable = 'yourStringValue'

SET @sqlQuery = 'SELECT * FROM tableName WHERE strValue = ' + '''' + '''' + @variable + '''' + ''''

SET @finalQuery = 'SELECT * FROM OPENQUERY(LINKEDSERVER,' + '''' + @sqlQuery + '''' + ')'

--used for debugging

SELECT @finalQuery

EXEC(@finalQuery)

NOTE: It may be hard to see but the '''' is 4 single quotes

I recommend commenting out EXEC(@finalQuery) until you see @finalQuery
correctly in the results.  Running SELECT @finalQuery is very helpful
because you can see what single quotes are missing if need be. 

I also recommend getting your OPENQUERY statement to work correctly
before trying to make it "dynamic" so to say using the above method.

Anyway, I found this very useful to use and hope that it can help some others out there.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: