看看语句错在那里
declare @m_t varchar(50)
declare @m_t1 varchar(50)
set @m_t='book'
set @m_t1='张三'
exec('select sum(总金额) from '+@m_t+' where 价格>=50 and
name in (select 编号 from buybook where name='+@m_t1+')')
执行报列名无效的错误
服务器: 消息 207,级别 16,状态 3,行 1
列名 '张三' 无效。
[解决办法]
糾正如下:
declare @m_t varchar(50)
declare @m_t1 varchar(50)
set @m_t='book'
set @m_t1='张三'
exec('select sum(总金额) from '''+@m_t+''' where 价格>=50 and
name in (select 编号 from buybook where name='+@m_t1+')')
[解决办法]
上面錯了,應是糾正如下:
declare @m_t varchar(50)
declare @m_t1 varchar(50)
set @m_t='book'
set @m_t1='张三'
exec('select sum(总金额) from '+@m_t+' where 价格>=50 and
name in (select 编号 from buybook where name='''+@m_t1+''')')
[解决办法]
運行以下你就會明白了:
declare @m_t varchar(50)
declare @m_t1 varchar(50)
set @m_t='book'
set @m_t1='张三'
select 'select sum(总金额) from '+@m_t+' where 价格>=50 and
name in (select 编号 from buybook where name='''+@m_t1+''')'
select 'select sum(总金额) from '+@m_t+' where 价格>=50 and
name in (select 编号 from buybook where name='+@m_t1+')'
------解决方案--------------------
两个引号是转义字符代表一个引号的
[解决办法]
写动态sql,链接字符串的时候一般都会用到转义字符的