I want to concatenate parameter value with string in a stored procedure. Because I want to complete select query: where condition. For that '@size_from' parameter must be concatenate with string, which contains select query.
My stored procedure:
dbo.GetDiamondDetail
ALTER PROCEDURE [dbo].[GetDiamondDetail]
@size_from numeric(18,2),
@size_to numeric(18,2),
@price_from int,
@price_to int,
@price_car_from int,
@price_car_to int,
@clarity_from nvarchar(6),
@clarity_to nvarchar(6),
@cut_from varchar(50),
@cut_to varchar(50),
@color_from varchar(50),
@color_to varchar(50),
@certi_lbl varchar(100),
@depth_from numeric(18,2),
@depth_to numeric(18,2),
@table_from numeric(18,2),
@table_to numeric(18,2),
@polish_from varchar(50),
@polish_to varchar(50)
AS
BEGIN
SET NOCOUNT ON;
Declare @Test Varchar(MAX)
Set @Test=' SELECT
dd.stock_no,
d.diamond_shape,
d.diamond_img,
dd.size,
c.cut_nm,
co.color_nm,
clr.clarity_nm,
dd.off_rap,
dd.price,
dd.price_per_carat,
(dd.price * 2) AS [total_price],
f.fluo_nm,
dd.depth,
dd.diamond_table,
dd.measurement,
cert.cert_nm,
p.polish_type,
s.symmetry_type,
dlr.dealer_email
FROM diamond_detail dd
JOIN diamond d ON dd.diamond_id=d.diamond_id
JOIN cut c ON dd.cut_id=c.cut_id
JOIN color co ON dd.color_id=co.color_id
JOIN clarity clr ON dd.clarity_id=clr.clarity_id
JOIN fluorescence f ON dd.fluo_id=f.fluo_id
JOIN certificate cert ON dd.cert_id=cert.cert_id
JOIN polish p ON dd.polish_id=p.polish_id
JOIN symmetry s ON dd.symmetry_id=s.symmetry_id
JOIN dealer dlr ON dd.dealer_id=dlr.dealer_id
JOIN width w ON dd.width_id=w.width_id
where '
set @Test=@Test+'dd.size_from>='+@size_from
PRINT @Test
EXEC (@Test)
END
Why make it so complicated and use strings? Just execute the statement:
ALTER PROCEDURE [dbo].[GetDiamondDetail]
@size_from numeric(18,2),
@size_to numeric(18,2),
@price_from int,
@price_to int,
@price_car_from int,
@price_car_to int,
@clarity_from nvarchar(6),
@clarity_to nvarchar(6),
@cut_from varchar(50),
@cut_to varchar(50),
@color_from varchar(50),
@color_to varchar(50),
@certi_lbl varchar(100),
@depth_from numeric(18,2),
@depth_to numeric(18,2),
@table_from numeric(18,2),
@table_to numeric(18,2),
@polish_from varchar(50),
@polish_to varchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT
dd.stock_no,
d.diamond_shape,
d.diamond_img,
dd.size,
c.cut_nm,
co.color_nm,
clr.clarity_nm,
dd.off_rap,
dd.price,
dd.price_per_carat,
(dd.price * 2) AS [total_price],
f.fluo_nm,
dd.depth,
dd.diamond_table,
dd.measurement,
cert.cert_nm,
p.polish_type,
s.symmetry_type,
dlr.dealer_email
FROM diamond_detail dd
JOIN diamond d ON dd.diamond_id=d.diamond_id
JOIN cut c ON dd.cut_id=c.cut_id
JOIN color co ON dd.color_id=co.color_id
JOIN clarity clr ON dd.clarity_id=clr.clarity_id
JOIN fluorescence f ON dd.fluo_id=f.fluo_id
JOIN certificate cert ON dd.cert_id=cert.cert_id
JOIN polish p ON dd.polish_id=p.polish_id
JOIN symmetry s ON dd.symmetry_id=s.symmetry_id
JOIN dealer dlr ON dd.dealer_id=dlr.dealer_id
JOIN width w ON dd.width_id=w.width_id
where dd.size_from >= @size_from;
END
change your code for where condition to handle null value, other wise while concatenation every thing goes to null and does not give the result.
First of all you must declare your temp variable as NVARCHAR
data type to accept unicode value.
--1. do not give where in first string, but check it.
if(isnull(@size_from,'') != ''
set @Test=@Test+' where dd.size_from>='+@size_from
PRINT @Test
EXEC (@Test)
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.