我有这个Proc
CREATE Proc [dbo].Salse_Ditail
-- Add the parameters for the stored procedure here
@Report_Form varchar(1) ,
@DateFrom datetime ,
@DateTo datetime ,
@COMPANYID varchar(3),
@All varchar(1) ,
@All1 varchar(1) ,
@All2 varchar(1) ,
@All3 varchar(1) ,
@All4 varchar(1) ,
@All5 varchar(1) ,
@Sector varchar(10),
@Report_Parameter nvarchar(max)
as
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @STRWhere nvarchar(max)
IF @All5=0 AND @All4=0 AND @All3=0 AND @All2=0 AND @All1=0 and @All=1
set @STRWhere= N'and Sector_id = @Sector'
if @Report_Form =1 or @Report_Form =3 or @Report_Form =4
SELECT RETURNREASONCODEID, SITE,SITE_NAME,Factory_id,Factory_Name,Sector_id,sector_name,Customer_name,
Customer_id,ITEMID,ITEMNAME,SALESMANID,SALESMAN_NAME,Net_Qty,Net_Salse,Gross_Sales,Gross_Qty,
NETWEIGHT_Gross,NETWEIGHT_salse_Gross,NETWEIGHT_NET,NETWEIGHT_salse_NET,Return_Sales,Free_Good,
CollectionAmount
FROM hal_bas_new_rep
WHERE DATAAREAID =@COMPANYID AND INVOICEDATE >= @DateFrom
AND INVOICEDATE <= @DateTo and Report_Activti = @Report_Form
if @Report_Form =2
SELECT RETURNREASONCODEID , RETURNREASONDESC, SITE , SITE_NAME , Factory_id ,
Factory_Name , Sector_id , sector_name , Customer_name , Customer_id ,
ITEMID , ITEMNAME , SALESMANID , SALESMAN_NAME , Return_Sales
FROM dbo.hal_bas_new_rep
WHERE DATAAREAID =@COMPANYID AND INVOICEDATE >= @DateFrom
AND INVOICEDATE <= @DateTo and Report_Activti = @Report_Form
and RETURNREASONCODEID in
(
SELECT Val
FROM dbo.fn_String_To_Table(@Report_Parameter,',',1)
)
/*
@STRWhere question how i can but the virble here
*/
end
GO
我想但是版本把变量放在Expression和的函数下面
我有很多功能我想添加
如果有人回答请发给我
答案 0 :(得分:1)
如果我理解正确,你想构造一个动态SQL语句然后执行它...它的一部分(where子句)是使用变量@STRWhere的字符串/文本内容。
您想要检查的是Execute
语句和名为sp_ExecuteSQL()
的系统存储过程
但你也可以像这样重写proc并做同样的事情:
If @Report_Form In (1,3,4)
SELECT RETURNREASONCODEID, SITE, SITE_NAME,
Factory_id, Factory_Name, Sector_id,
sector_name,Customer_name, Customer_id,
ITEMID, ITEMNAME, SALESMANID, SALESMAN_NAME,
Net_Qty, Net_Salse, Gross_Sales,G ross_Qty,
NETWEIGHT_Gross, NETWEIGHT_salse_Gross,
NETWEIGHT_NET, NETWEIGHT_salse_NET,
Return_Sales,Free_Good, CollectionAmount
FROM hal_bas_new_rep
WHERE DATAAREAID = @COMPANYID
And INVOICEDATE Between @DateFrom And @DateTo
And Report_Activti = @Report_Form
If @Report_Form =2
SELECT RETURNREASONCODEID, RETURNREASONDESC, SITE,
SITE_NAME, Factory_id, Factory_Name, Sector_id,
sector_name, Customer_name, Customer_id,
ITEMID, ITEMNAME, SALESMANID, SALESMAN_NAME,
Return_Sales
FROM dbo.hal_bas_new_rep
WHERE DATAAREAID =@COMPANYID
And INVOICEDATE Between @DateFrom And @DateTo
And Report_Activti = @Report_Form
And RETURNREASONCODEID in
(SELECT Val From dbo.fn_String_To_Table(@Report_Parameter,',',1))
And Sector_id = Case When @All=1 And @All5+@All4+@All3+@All2=+@All1=0
Then @Sector Else Sector_id End
结束