日期:2014-05-18 浏览次数:20407 次
EXECUTE au_info 'firstname',NULL --或者 EXECUTE au_info NULL,'lastname'
------解决方案--------------------
如果一定要用一个参数,就将存储过程改成如下:
CREATE PROCEDURE au_info @name varchar(40) AS SELECT au_lname, au_fname, title, pub_name FROM authors a INNER JOIN titleauthor ta ON a.au_id = ta.au_id INNER JOIN titles t ON t.title_id = ta.title_id INNER JOIN publishers p ON t.pub_id = p.pub_id WHERE au_fname = @name ORageCount
------解决方案--------------------
下面的方法当 EXECUTE au_info 'firstname' 执行时,可以成功,但是此时'lastname'只能加到@lastname上,所以想要 @firstname 复制,只能EXECUTE au_info null, 'lastname'这样
CREATE PROCEDURE au_info @lastname varchar(40)=null, @firstname varchar(20)=null AS SELECT au_lname, au_fname, title, pub_name FROM authors a INNER JOIN titleauthor ta ON a.au_id = ta.au_id INNER JOIN titles t ON t.title_id = ta.title_id INNER JOIN publishers p ON t.pub_id = p.pub_id WHERE au_fname = @firstname AND au_lname = @lastname
------解决方案--------------------
很简单.我假设,你存储过程的两个参数为 @p1,@p2
那么依具你存储过程中的写法,也有不同的写法.
--1,若是采用动态语句 declare @sql varchar(1000) set @sql='select * from tb where 1=1 ' if @p1 is not null set @sql=@sql + ' and field1=' + @p1 if @p2 is not null set @sql=@sql + ' and field2=' + @p2 --2,若是采用的非动态语句 select * from tb where field1=case when @p1 is null then field1 else @p1 end and field2=case when @p2 is null then field2 else @p2 end
------解决方案--------------------
CREATE PROCEDURE au_infoge