存储过程的字段名用变量替代,不是用'@变量名'么
存储过程的字段名用变量代替,不是用'@变量名'么?
存储过程的字段名用变量代替,不是用'@变量名'么?
select * FROM DisciplineInfo WHERE @fild=@valu;
和 select * FROM DisciplineInfo WHERE '+@fild+'=@valu;
这两个都不好使,怎么回事啊
------解决方案--------------------
字段名用变量需要动态执行。
exec('select * from tb where '+@fild+'='+@value)
------解决方案--------------------
存储过程的字段名用变量代替,不是用'@变量名'么?
select * FROM DisciplineInfo WHERE @fild=@valu;
和 select * FROM DisciplineInfo WHERE '+@fild+'=@valu;
这两个都不好使,怎么回事啊
------解决方案--------------------
字段名用变量需要动态执行。
exec('select * from tb where '+@fild+'='+@value)
------解决方案--------------------
- SQL code
exec sp_executesql N'select * FROM DisciplineInfo WHERE @fild=@valu;' ,N'@fild nvarchar(100),@valu nvarchar(50)' ,@fild ,@valu;
------解决方案--------------------
select * FROM DisciplineInfo WHERE fild=@valu;
由以上看出 你的fild字段应该是字符类型
------解决方案--------------------
set @sql='select top '+str(@field)+' '+@key+' from '+@table+@orderby
exec(@sql)
------解决方案--------------------
字段不能用@定义 参数才是需要@
------解决方案--------------------
为什么字段名要用变量代替呢,直接使用组合条件不是更好?
create table #t(id int, name varchar(10));
insert into #t
select 1, 'zhangsan' union all
select 2, NULL
declare @field varchar(10)
set @field = 'id'
declare @value int
set @value = 1
declare @execSql varchar(500)
set @execSql = ' select * from #t where {0} = ''{1}'' '
set @execSql = Replace(@execSql, '{0}', @field)
set @execSql = Replace(@execSql, '{1}', @value)
SELECT @execSql
EXEC(@execSql)
drop table #t
go
还是不理解你为什么要使用动态字段名……
------解决方案--------------------
把整条语句拼接成字符串 ,执行即可以了