0
我的問題是我怎樣才能以智能的方式提高我的查詢字符串?T-SQL動態查詢
我想使用參數查詢來自數據庫的數據,當參數爲空時,它將被刪除。
我的查詢字符串如下喜歡:
ALTER procedure [dbo].[spQueryDataFromCPREFROM]
@CompanyName varchar(50),
@TestDDL varchar(20),
@TesterName varchar(50),
@DatepickerForTestDate datetime,
@ApplyDate datetime,
@TestTypeDDL varchar(1),
@QueryString nvarchar(max)
as
begin
if @CompanyName is not null
and @TestDDL is not null
and @TesterName is not null
and @DatepickerForTestDate is not null
and @ApplyDate is not null
and @TestTypeDDL is not null
and @QueryString is not null
begin
select @QueryString = 'select * from [dbo].[CPREFROM] as CF where CF.[UNIT_TID] ='+ @CompanyName
+'and CF.[ELIGIBLE_RATING] ='+ @TestDDL
+'and CF.[Name] =' + @TesterName
+'and CF.[CHECKDATE] ='+ @DatepickerForTestDate
+'and CF.[APPLYDATE] =' + @ApplyDate
+'and CF.[A_S] =' + @TestTypeDDL
end
else if @CompanyName is null
and @TestDDL is not null
and @TesterName is not null
and @DatepickerForTestDate is not null
and @ApplyDate is not null
and @TestTypeDDL is not null
and @QueryString is not null
begin
select @QueryString = 'select * from [dbo].[CPREFROM] as CF where CF.[ELIGIBLE_RATING] ='+ @TestDDL
+'and CF.[Name] =' + @TesterName
+'and CF.[CHECKDATE] ='+ @DatepickerForTestDate
+'and CF.[APPLYDATE] =' + @ApplyDate
+'and CF.[A_S] =' + @TestTypeDDL
end
...
是的,你的答案是真棒感謝。 –