Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Frnds,

I want to give "Where" condition in this query were i give this condition i don't know. please help me.

SQL
DECLARE @cols AS NVARCHAR(MAX),
@colsName AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(ItemCode +'_'+c.col)
from Vw_InOutWard
cross apply
( select 'TotalCount' col
union all
select 'TotalAmount') c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
select @colsName = STUFF((SELECT distinct ', ' + QUOTENAME(ItemCode +'_'+c.col) +' as [' + ItemCode + case when c.col = 'TotalCount' then ']' else ' Rate]' end
from Vw_InOutWard
cross apply
(select 'TotalCount' col
union all
select 'TotalAmount') c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = 'SELECT DCNo,DCDate,FromSupName,FromAddress,FromContNo,ToSupName,ToAddress,ToContNo,RefferedBy, ' + @colsName + '
from
(select DCDate, DCNo,FromSupName,FromAddress,FromContNo,ToSupName,ToContNo,RefferedBy,ToAddress, ItemCode +''_''+col col, value
from
(select DCDate, ItemCode, DCNo,FromSupName,FromAddress,FromContNo,ToSupName,ToAddress,ToContNo,RefferedBy,
cast(Qty as numeric(10, 2)) totalcount,
cast(Rate as numeric(10, 2)) totalamount
from Vw_InOutWard) src
unpivot
(value for col in (totalcount, totalamount)) unpiv ) s
pivot (sum(value) for col in (' + @cols + ')) p '
execute(@query)
Posted
Updated 11-Oct-15 19:57pm
v3
Comments
Maciej Los 12-Oct-15 1:58am    
Somewhere between ''...
Vivek.anand34 12-Oct-15 2:43am    
actually here dc no field is there.. i need to retrieve Where DCNo='DC-001'
or give parameter @dcno then how to call this paramter in asp .net c# code i.e, textbox value.

1 solution

Pass DC No as a parameter from frontend and change set @query as below

SQL
set @query = 'DECLARE @DCNo INT;
SET @DCNo ='+Cast(@DCNO as VARCHAR)+';
SELECT DCNo,DCDate,FromSupName,FromAddress,FromContNo,ToSupName,ToAddress,ToContNo,RefferedBy, ' + @colsName + '
from
(select DCDate, DCNo,FromSupName,FromAddress,FromContNo,ToSupName,ToContNo,RefferedBy,ToAddress, ItemCode +''_''+col col, value
from
(select DCDate, ItemCode, DCNo,FromSupName,FromAddress,FromContNo,ToSupName,ToAddress,ToContNo,RefferedBy,
cast(Qty as numeric(10, 2)) totalcount,
cast(Rate as numeric(10, 2)) totalamount
from Vw_InOutWard WHERE DCNo=@DCNO ) src
UNPIVOT
(value for col in (totalcount, totalamount)) unpiv ) s
pivot (sum(value) for col in (' + @cols + ')) p '
 
Share this answer
 
Comments
Vivek.anand34 12-Oct-15 3:38am    
ya i put tat line.. but underlined the @DCNO ' must declare the scalar variable @DCNO '
Abdul Samad KP 12-Oct-15 4:22am    
You need to pass the @DCNo from frontend, ie your textbox value, and if it is not a integer then change set @query = 'DECLARE @DCNo INT;
to set @query = 'DECLARE @DCNo varchar;

By the way I assume , you put all this query in a stored procedure
Vivek.anand34 12-Oct-15 5:56am    
Iput this line in front end:
cmd.Parameters.AddWithValue("@DcNo", txtDCNo.Text);

object reference not set an instance of an object is displayed
Abdul Samad KP 12-Oct-15 7:45am    
Pls post full code
Vivek.anand34 12-Oct-15 8:19am    
cmd = new SqlCommand("TEST", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@dc", txtDCNo.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900