首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请教一个SQL语句~

2012-01-12 
请问一个SQL语句~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~select * from 表A where @

请问一个SQL语句~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
select * from 表A where @name=@value 这样写SQL语句可行吗?? @name和@value是参数

[解决办法]
你的意思是字段和值都作为参数吗?
如果是的话。只能用动态SQL

SQL code
create proc wsp@name varchar(50),@value varchar(50)as   declare @sql varchar(8000)   set @sql='select * from 表A where ' + @name + '=' + @value    exec(@sql)调用存储过程:exec wsp '',''
[解决办法]
2楼的做法可行。
不过需要进行数据类型的判断,对于文本类型的
@value要两头加"'"号。
SQL code
declare @type varchar(50)select @type=name from systypes where xtype=(    select xtype from syscolumns where id=object_id(表名) and name=@field)if charindex('char',@type,1)>0 or charindex('text',@type,1)>0 or charindex('date',@type,1)>0    这是文本类型的else    非文本类型的
[解决办法]
上面的代码针对于@value值为文本型,如果你的@value值为数值型的话,就可以直接这样:
SQL code
--下面例子使用NorthWind数据库use NorthWinddeclare @name varchar(10)declare @value varchar(10)declare @sql varchar(1000)set @name='EmployeeID'set @value='9'select @sql = 'select * from Orders where '+@name+'='+@valueexec(@sql)   print @sql 

热点排行