?
Solved

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

Posted on 2011-09-26
9
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 57

Expert Comment

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

single " --> double '
0
 
LVL 57

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 57

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 57

Accepted Solution

by:
HainKurt earned 1800 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 200 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 57

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

771 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