如何为此查询创建存储过程?
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
你的 like 语句中有多余的引号 像这样更改你的语句
' 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
我想为这个查询写一个存储过程!
我的查询是:
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
你的 like 语句中有多余的引号 像这样更改你的语句
' 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