簡體   English   中英

如何為此查詢創建存儲過程?

[英]how to create stored procedure for this query?

我想為此查詢編寫一個存儲過程!

我的查詢是:

SELECT *
FROM [dbo].[Table_asbabbazi] 
WHERE
    name_product LIKE '%'+'ibm'+ '%' 
    AND first_price BETWEEN 5000 AND 100000  
    AND collection_1 = 'collection1'  
    AND id_state = 8

我寫了這樣的動態存儲過程:

ALTER PROCEDURE  [dbo].[Asbabbazi_A]
    @name_product nvarchar(50),
    @first_price int,
    @final_price int,
    @collection_1 nvarchar(30),
    @id_state tinyint
AS
BEGIN
    DECLARE @SQLstring nvarchar(1000)
    DECLARE @PARAMS nvarchar(1000)  

    SET @SQLstring = 'SELECT IDproduct, name_product, first_price, final_price, max_registered_price, final_date_view_shamsi, count_views, image_1 FROM dbo.Table_asbabbazi WHERE active= 0 '

    if(@name_product != 'no name')
      set @SQLstring = @SQLstring + ' AND name_product  LIKE '''+ '%' + @name_product + '%' + ''''

    if (@final_price != 0)
      set @SQLstring = @SQLstring +  ' AND  first_price between  @first_price  AND  @final_price'

    if (@collection_1 != 'انتخاب کنید')
       set @SQLstring = @SQLstring + ' AND collection_1 =  @collection_1'

    if (@id_state != 0)
       set @SQLstring = @SQLstring + ' AND id_state =  @id_state '  
    set @PARAMS='@name_product nvarchar(50),
    @first_price int,
    @final_price int,
    @collection_1 nvarchar(30),
    @id_state tinyint'

    EXECUTE sp_executesql @SQLstring, @PARAMS, @name_product, @first_price, @final_price, @collection_1, @id_state
END

該存儲過程有效,但是存在一個問題: name_product設置值顯示一個產品或任何產品。 我在SQL Server Management Studio中測試查詢,它可以正常工作。 但是存儲過程中的此查詢無法正常工作。 我認為問題在這一行

 if(@name_product != 'no name')
    set @SQLstring = @SQLstring + ' AND name_product  LIKE '''+ '%' + @name_product + '%' + ''''

請幫忙

像這樣更改查詢:

使用print @SQLstring進行調試查詢

DECLARE @SQLstring nvarchar(1000)
DECLARE @PARAMS nvarchar(1000)  
set @SQLstring = 'SELECT IDproduct,name_product,first_price,final_price,max_registered_price,
                 final_date_view_shamsi,
                   count_views,image_1 from dbo.Table_asbabbazi where active= 0 '
if(@name_product != 'no name')
set @SQLstring = @SQLstring + ' AND name_product  LIKE '''+ '%' + @name_product + '%' + ''''
if (@final_price != 0)
set @SQLstring = @SQLstring +  ' AND  first_price between  '+CAST(@first_price as nvarchar(10))+'  AND  
'+CAST(@final_price as nvarchar(10))+''
if (@collection_1 != 'انتخاب کنید')
set @SQLstring = @SQLstring + ' AND collection_1 =  '''+@collection_1 +''' '
if (@id_state != 0)
set @SQLstring = @SQLstring + ' AND id_state = '+CAST(@id_state as nvarchar(10)) 
set @PARAMS='@name_product nvarchar(50),
    @first_price int,
    @final_price int,
    @collection_1 nvarchar(30),
    @id_state tinyint'

print @SQLstring
EXECUTE sp_executesql @SQLstring,
@PARAMS,
@name_product,
@first_price,
@final_price,
@collection_1,
@id_state

您的喜歡聲明中有多余的引號,您可以這樣改變您的聲明

' AND name_product  LIKE ''%' + @name_product + '%'''

這是您的完整SP

ALTER PROCEDURE  [dbo].[Asbabbazi_A]
@name_product nvarchar(50),
@first_price int,
@final_price int,
@collection_1 nvarchar(30),
@id_state tinyint
AS
BEGIN
DECLARE @SQLstring nvarchar(1000)
DECLARE @PARAMS nvarchar(1000)  
set @SQLstring = 'SELECT IDproduct,name_product,first_price,final_price,max_registered_price,
             final_date_view_shamsi,
               count_views,image_1 from dbo.Table_asbabbazi where active= 0 '
if(@name_product != 'no name')
set @SQLstring = @SQLstring + ' AND name_product  LIKE ''%' + @name_product + '%'''
if (@final_price != 0)
set @SQLstring = @SQLstring +  ' AND  first_price between  @first_price  AND     @final_price'
if (@collection_1 != 'انتخاب کنید')
set @SQLstring = @SQLstring + ' AND collection_1 =  @collection_1' 
if (@id_state != 0)
set @SQLstring = @SQLstring + ' AND id_state =  @id_state ' 
set @PARAMS='@name_product nvarchar(50),
@first_price int,
@final_price int,
@collection_1 nvarchar(30),
@id_state tinyint'
EXECUTE sp_executesql @SQLstring,
@PARAMS,
@name_product,
@first_price,
@final_price,
@collection_1,
@id_state
END

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM