链接服务器开启SQL传参的写法参考
需求描述:一个模板中需要通过外部数据源链接服务器提取数据,提取数据时需要将本报表主表值作为条件参数过滤来源数据。可以按下面的方法先拼接好执行的语句,再执行。
参考1:
- DECLARE @NAME VARCHAR(4000)
- DECLARE @SQL VARCHAR(4000)
- SET @NAME = 'zm'
- SET @SQL = 'select * from openquery(ab,''select * from sys_user where loginname = '''''+@NAME+''''''')'
- EXEC(@SQL)
复制代码
参考2:
- 定义变量 @NAME VARCHAR(4000)
- 定义变量 @SQL VARCHAR(4000)
- 设置变量值 @NAME = 本报表.[OPENQUERY_主表].[名称]
- 设置变量值 @SQL = '
- SELECT
- [LID] AS 本报表.[OPENQUERY_明细表].[LID]x填充不锁定,
- [COMPUTERNAME] AS 本报表.[OPENQUERY_明细表].[COMPUTERNAME]x填充不锁定,
- [USERNAME] AS 本报表.[OPENQUERY_明细表].[USERNAME]x填充不锁定,
- [LOGTYPE] AS 本报表.[OPENQUERY_明细表].[LOGTYPE]x填充不锁定,
- [LOGDATE] AS 本报表.[OPENQUERY_明细表].[LOGDATE]x填充不锁定,
- [LOGDETAIL] AS 本报表.[OPENQUERY_明细表].[LOGDETAIL]x填充不锁定
- FROM OPENQUERY(VMSQL,
- ''SELECT
- SYS_VIEW_LOG.LID ,
- SYS_VIEW_LOG.COMPUTERNAME ,
- SYS_VIEW_LOG.USERNAME ,
- SYS_VIEW_LOG.LOGTYPE ,
- SYS_VIEW_LOG.LOGDATE ,
- SYS_VIEW_LOG.LOGDETAIL
- FROM SYS_VIEW_LOG WHERE USERNAME = '''''+@NAME+''''''')'
- EXEC(@SQL)
复制代码
|
|
hessen