Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
declare @accNo nvarchar(1000)
declare @ParmDefinition nvarchar(1000)
DECLARE @SQLString NVARCHAR(500);
set @accNo =''
SET @ParmDefinition = N'@accNo1 nvarchar(1000)'
declare @amount money
Declare @temp as int
Declare @bl as tinyint
Set @bl=1
DECLARE db_cursor CURSOR FOR  
select rcb.gl_accno_cbs into #temp from particulars_reports pr
Inner Join reports rp on pr.rpt_id=rp.rpt_id
Inner Join particulars p on p.particular_id=pr.particular_id
Inner Join rmu_cbs_border rcb on rcb.particular_id=pr.particular_id
Where rp.rpt_id=3 and p.particular_id=9

	OPEN db_cursor   
		FETCH NEXT FROM db_cursor INTO @temp  
		WHILE @@FETCH_STATUS = 0  
		BEGIN 
		
		if @bl=1
		begin 
		Set @accNo= @accNo+''''+ CAST(@temp as CHAR(6)) +''''
		end
		else 
		begin
		Set @accNo=@accNo+','+ '''' + CAST(@temp as CHAR(6)) +''''
		end 
		Set @bl=2

	FETCH NEXT FROM db_cursor INTO @temp
	END  

CLOSE db_cursor  
	DEALLOCATE db_cursor
	select @accNo

select @amount
set @SQLString=N'Select sum(case when fmgl.dr_cr=''DR'' then fmgl.amount_tk else -amount_tk end) as Amount
From balance fmgl
Where fmgl.tdate< ''30-Sep-2010'' and fmgl.accountno in (@accNo1)';							
EXECUTE sp_executesql @SQLString,@ParmDefinition,@accNo1 = @accNo

This is my code
****************************
in @accNo variable i get value '1','5','6'
if put harcode value as '1','5','6' i get amount but if i put @accNo variable i get null.

I can get the reusult using Inner join but i need it to make faster query

[edit]Code block added, urgency removed. It may be urgent for you, but it isn't to us. It just annoys some people and can slow down responses. - OriginalGriff [/edit]
Posted
Updated 9-Feb-11 20:46pm
v2

Hi,

I see the issue is in dynamic sql you building at bottom. Re write the sql like follow and it should be ok

SQL
set @SQLString=N'Select sum(case when fmgl.dr_cr=''DR'' then fmgl.amount_tk else -amount_tk end) as Amount
From balance fmgl
Where fmgl.tdate< ''30-Sep-2010'' and fmgl.accountno in ('+@accNo1+')';


Hope this will help
 
Share this answer
 
Comments
juwel115 10-Feb-11 3:51am    
i doesnt work. Thanks for reply
Shahriar Iqbal Chowdhury/Galib 10-Feb-11 4:29am    
what is the value in @accNo1 ?
Hi,

You cannot use a string variable in IN clause like that. One way would be that you concatenate the string value in @accNo1 to your SQL statement, but I don't suggest you do that. Instead place the query to get account numbers in your summary statement. Something like:
set @SQLString=N'Select sum(case when fmgl.dr_cr=''DR'' then fmgl.amount_tk else -amount_tk end) as Amount
From balance fmgl
Where fmgl.tdate < ''30-Sep-2010'' 
and fmgl.accountno in (select rcb.gl_accno_cbs 
                       from particulars_reports pr
                       Inner Join reports rp on pr.rpt_id=rp.rpt_id
                       Inner Join particulars p on p.particular_id=pr.particular_id
                       Inner Join rmu_cbs_border rcb on rcb.particular_id=pr.particular_id
                       Where rp.rpt_id=3 and p.particular_id=9)';


Regards,

mika
 
Share this answer
 
Comments
juwel115 10-Feb-11 3:46am    
i did this but it take 24 sec where as if i can put account in('1','2','3') it takes less than 2 sec
Wendelius 10-Feb-11 4:03am    
Check that the inner query still returns the correct rows (1, 5 and 6) just to make sure that there are no editing errors.

If the query is still slow, the next thing would be to concentrate on query plan. Check the plan and make sure that you have correct indexes for the tables. Based on your query potential indexes could be:
- on Balance table index for AccountNo, TDate
- on Reports table index for Rpt_Id
- on Particulars table index for Particular_Id
- and indexes for all used foreign/primary keys
juwel115 10-Feb-11 5:22am    
Thanks mika. i have done it using string concatenate.
i used string concatenate because this is small portion of my
SP if this portion takes 24 second thn my SP takes 6 min.
now it takes 1.3 min
Wendelius 10-Feb-11 5:35am    
You're welcome :)

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