我在将字符串(12 \ 11 \ 2013格式)转换为动态sql中的Datetime('YYYY-MM-DD'格式)时遇到问题。
您可以在下面看到相关示例和结果: 声明@dbPrefix varchar(2), @cob varchar(20), @Sql varchar(200) 声明@m_cobMinusOne datetime,
@m_cobEOM varchar(20),
@m_cobEOY varchar(20),
@m_cobFUN varchar(20)
set @cob = '12/11/2013'
set @dbPrefix='q_'
set @m_cobMinusOne = convert(varchar(20),@m_cobMinusOne,103)
set @m_cobEOM = convert(varchar(20),@m_cobMinusOne,103)
exec(N'select ' + @m_cobMinusOne + '= bdh_prev_bus_d ,' + @m_cobEOM + '= bdh_prev_mo_end_d
from ' +@dbPrefix+'dbbdm005..bdh_bus_date_ref where bdh_bus_d = ''' + @cob + '''')
答案 0 :(得分:0)
看看你可以做这样的事情......
declare
@dbPrefix varchar(2),
@cob varchar(20) ,
@m_cobMinusOne datetime,
@m_cobEOM varchar(20),
@SqlQuery NVARCHAR(MAX);
set @cob = '20131211';
set @dbPrefix = 'q_';
set @m_cobMinusOne = convert(varchar(20),@m_cobMinusOne,103);
set @m_cobEOM = convert(varchar(20),@m_cobMinusOne,103);
SET @SqlQuery = N'select @m_cobMinusOne = bdh_prev_bus_d , @m_cobEOM = bdh_prev_mo_end_d ' +
N'from ' + QUOTENAME(@dbPrefix+'dbbdm005') +'..bdh_bus_date_ref ' +
N'where bdh_bus_d = @cob';
EXECUTE sp_executesql @SqlQuery
,N'@m_cobMinusOne VARCHAR(20) OUTPUT, @m_cobEOM VARCHAR(20) OUTPUT, @cob VARCHAR(20)'
,@m_cobMinusOne OUTPUT
,@m_cobEOM OUTPUT
,@cob