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.
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.
相关文章推荐
- Linux学习之telnet命令
- 阿里云Centos端口转发
- centos远程访问mssql数据库
- linux下的gdb调试工具--内存调试
- linux的软链接
- linux 修改终端颜色
- linux命令安装
- 【linux】命令
- linux安装mysql
- Linux环境下提升普通用户权限(sudo)
- linux 怎么样复制文件夹内所有文件到另一个文件夹?
- Linux meld安装
- Linux学习netstat
- linux常用基本命令
- linux 增加sudo权限
- linux-0.12/boot/bootsect.S下的常见问题
- linux下后台启动weblogic服务(还有些注意事项)
- Centos 安装Rsync笔记
- centos 服务器装与python34源码安装
- CentOS/RHEL6.5中使用WordPress快速建站