在运行时组合查询

时间:2012-05-14 11:13:33

标签: c# .net

我想问一下,如果可以在运行时组合SQL查询。

假设我有一个包含参数@type(int)@name(varchar(50))=null@dept(varchar(50))=null的存储过程。

现在我有一个查询Select * from Employees

我的问题是,当@type = 2时,我可以动态添加where name = @name或{@ 1}}时的动态添加

3 个答案:

答案 0 :(得分:0)

你可以写

where (@type = 1 and name = @name) or (@type = 2 and dept = @dept)

答案 1 :(得分:0)

是的,你可以这样做 以下链接显示了如何在存储过程中使用参数。 http://www.codeproject.com/Articles/126898/Sql-Server-How-to-write-a-Stored-procedure-in-Sql

答案 2 :(得分:0)

相关问题