Solved

sql server, passing table name dynamically to the query stmt.

Posted on 2011-09-26
9
227 Views
Last Modified: 2012-05-12
I have the following code and I get all sort of errors related to the conversion of data type from varchar to int,...

now things like Weight is of type float in this table.

I do not know what is wrong but can not get it run. even when I exclude all problematic fields, I get error message related to execute line....

Any way to get his working?

Any other way of passing parameters to query?

thanks in advance

Declare 
  @loopcntr int
, @ID	 int
, @desName nvarchar(20)
, @srcName nvarchar(20)
, @Period  int
, @country nvarchar(4)
, @preloc  nvarchar(3)
, @strSql	nvarchar(4000)

Create Table #patient(
	emp_ID			int identity(1,1) not null
,	ID				int
,	srcName		nvarchar(30)
,	desName		nvarchar(30)
,	Country		nvarchar(3)
,   preloc      nvarchar(3)
,	period		int
)

Insert into #Empoyee
select ID, srcName, desName, Country , preloc , period
from TblSource where desName = 'Empoyee'


Set @loopcntr = @@ROWCOUNT 

While (@loopcntr <> 0)
Begin			
		set @loopcntr = 1
		Set @ID	= (Select ID From #Empoyee Where emp_ID = @loopcntr)
						
		if (@ID <> '')
		Begin
						
		Set @desName	= (Select [desname]	from #Empoyee Where emp_ID = @ID)		
		Set @srcName	= (Select [srcName]	from #Empoyee Where emp_ID = @ID)	
		Set @Period		= (select [Period]	from #Empoyee Where emp_ID = @ID)
		Set @country	= (select [country]	from #Empoyee Where emp_ID = @ID)
		Set @preloc	= (select [preloc]	from #Empoyee Where emp_ID = @ID)

Select @strSql = 'INSERT INTO MYDB.dbo.[' + @desName + '] (
		[EmployeeNum] 
	,	[Period]  
	,	[Location]	
	,   [AGE]
	,	[Gender_ID]
	,	[Weight]  
	,	[Salary]
	,	[Bonus1] 
	,	[Bonus2]
	,   [country]	 
)
	
	SELECT
		[employee#]
    ,   ' + ISNULL(@Period , '') + '
    ,   (' + @prefix + '[location])	AS site	
	,   [AGE]
	,	(CASE WHEN Gender = "F" THEN  1 ELSE  2 END)	AS [Gender]	
	,	' + ISNULL( + '[WEIGHT],' + null + ') AS [WEIGHT]
   ,	[Salary]
	,	[Bonus1] 
	,	[Bonus2]	
	,	' + @Country_Code + '
		
	From AI_DEV_Test.dbo.[' + @srcName + ']'

	Execute sp_executesql @strSql
	
	End
	set @loopcntr = @loopcntr -1 


End

Open in new window

0
Comment
Question by:shmz
9 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 36658705
CASE WHEN Gender = "F" THEN  
-->
CASE WHEN Gender = ''F'' THEN  

single " --> double '
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36659678
this loop is infinite?

While (@loopcntr <> 0)
Begin                  
  set @loopcntr = 1 -- looks like you need to remove this first
  ...
  set @loopcntr = @loopcntr -1
end;

I guess it should be set to rowcount first (which you do I guess)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36660446
also

            Set @desName      = (Select [desname]      from #Empoyee Where emp_ID = @ID)            
            Set @srcName      = (Select [srcName]      from #Empoyee Where emp_ID = @ID)      
            Set @Period            = (select [Period]      from #Empoyee Where emp_ID = @ID)
            Set @country      = (select [country]      from #Empoyee Where emp_ID = @ID)
            Set @preloc      = (select [preloc]      from #Empoyee Where emp_ID = @ID)
-->

            Set @desName = [desname], @srcName      = [srcName], ... from #Empoyee Where emp_ID = @ID
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 51

Accepted Solution

by:
HainKurt earned 450 total points
ID: 36662994
and maybe this
Select @strSql = 'INSERT INTO MYDB.dbo.[' + @desName + '] (
		[EmployeeNum] 
	,	[Period]  
	,	[Location]	
	,   [AGE]
	,	[Gender_ID]
	,	[Weight]  
	,	[Salary]
	,	[Bonus1] 
	,	[Bonus2]
	,   [country]	 
) SELECT
		[employee#]
    ,   ''' + ISNULL(@Period , '') + '''
    ,   (' + @prefix + '[location])	AS site	
	,   [AGE]
	,	(CASE WHEN Gender = ''F'' THEN  1 ELSE  2 END) AS [Gender]	
	,	ISNULL([WEIGHT], null) AS [WEIGHT]
    ,	[Salary]
	,	[Bonus1] 
	,	[Bonus2]	
	,	' + @Country_Code + '
	From AI_DEV_Test.dbo.[' + @srcName + ']';

Open in new window

0
 

Author Comment

by:shmz
ID: 36675049
Hi HainKurt,

- set @loopcntr = 1 -- looks like you need to remove this first
wasn't suppose to be there just a typo.

-  Set @desName = [desname], @srcName      = [srcName], ... from #Empoyee Where emp_ID = @ID
Thanks for the tip..

- I'll try your query tomorrow and will get back to you.

Many thanks...It was so frustrating and you made it look so simple! :)
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 50 total points
ID: 36677898
typo alert:


            Set @desName = [desname], @srcName      = [srcName], ... from #Empoyee Where emp_ID = @ID


-->        Select @desName = [desname], @srcName      = [srcName], ... from #Empoyee Where emp_ID = @ID
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36707641
Before execute just print and see the query is build proper. copy paste the query and execute it. it will show you all errors.

One thing when you uses inline query use ('') two single quote in place of single. and when you pass char values use 3 quotes.

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36710799
please post the latest code each time with the error message...
0
 

Author Closing Comment

by:shmz
ID: 36899844
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql Data via Excel--performance issues 2 55
SSRS report parameters set after publishing to report manager 1 55
Unable to save view in SSMS 21 69
Sql Server group by 10 26
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question