?
Solved

Variable Tables in Stored Procedures

Posted on 2006-06-26
10
Medium Priority
?
360 Views
Last Modified: 2012-08-14
I get  error message(below)  when I run query below.  I declared employee2 table as  Variable table.  I am comparing employee2 and employee3  tables.  Employee2 table is a variable table.  Any ideas?  I'm using SQl 2000
-----------------------------------------------------------------  
Server: Msg 137, Level 15, State 2, Procedure ComparTables, Line 32
Must declare the variable '@employee2'.
-----------------------------

CREATE PROCEDURE ComparTables AS

DECLARE @Employee2 TABLE
 (
  EmployeeID  int(4),
  Fullname     varchar(32),
  Salary         int (4)
  )


declare @SQL varchar(8000)

   , @T1ColumnList varchar(1000)
   , @T2ColumnList varchar(1000)
   , @employee3    Varchar (100)


Set @t1ColumnList = 'employeeID,fullname, Salary'
Set @t2Columnlist = 'employeeID,fullname, Salary'


IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList

set @SQL = 'SELECT ''' + @employee2 + ''' AS TableName, ' + @t1ColumnList +

 ' FROM ' + @employee2 + ' UNION ALL SELECT ''' + @employee3 + ''' As TableName, ' +

 @t2ColumnList + ' FROM ' + @employee3

set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +

 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +

 ' HAVING COUNT(*) = 1'

exec ( @SQL)

0
Comment
Question by:Aopoku
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 16987219
You cannot fill a table variable with dynamic sql, unless you make the table variable part of the dynamic sql.
on the other hand, you will not be able to use the table variable outside the dynamic sql if putting it into the dynamic sql.

Solutions:
* either you can do ALL the work with the table variable inside the dynamic sql
* or you need to use temp tables instead
* or use a non-dynamic-sql solution


CREATE PROCEDURE ComparTables AS

declare @SQL varchar(8000)
   , @T1ColumnList varchar(1000)
   , @T2ColumnList varchar(1000)
   , @employee3    Varchar (100)


Set @t1ColumnList = 'employeeID,fullname, Salary'
Set @t2Columnlist = 'employeeID,fullname, Salary'


IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList

set @SQL = 'DECLARE @Employee2 TABLE
 (
  EmployeeID  int(4),
  Fullname     varchar(32),
  Salary         int (4)
  )

SELECT ''' + @employee2 + ''' AS TableName, ' + @t1ColumnList +

 ' FROM ' + @employee2 + ' UNION ALL SELECT ''' + @employee3 + ''' As TableName, ' +

 @t2ColumnList + ' FROM ' + @employee3

set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +

 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +

 ' HAVING COUNT(*) = 1'

exec ( @SQL)

0
 

Author Comment

by:Aopoku
ID: 16987880
Still comes up with error must declare @employee2
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 16989205
try this...
CREATE PROCEDURE ComparTables AS

declare @SQL varchar(8000)
   , @T1ColumnList varchar(1000)
   , @T2ColumnList varchar(1000)
   , @employee3    Varchar (100)


Set @t1ColumnList = 'employeeID,fullname, Salary'
Set @t2Columnlist = 'employeeID,fullname, Salary'


IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList

set @SQL = 'DECLARE @Employee2 TABLE
 (
  EmployeeID  int(4),
  Fullname     varchar(32),
  Salary         int (4)
  )

SELECT '' + @employee2 + '' AS TableName, ' + @t1ColumnList +

 ' FROM  + @employee2 +  UNION ALL SELECT ''' + @employee3 + ''' As TableName, ' +

 @t2ColumnList + ' FROM ' + @employee3

set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +

 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +

 ' HAVING COUNT(*) = 1'

exec ( @SQL)


0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 12

Expert Comment

by:Einstine98
ID: 16989212
You need to treat @employee2 as part of the dynamic SQL code... I'm not sure what is the purpose of your code as you are checking for @t2columnlist being empty right after you are setting it's value!

can you explain what you are after?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16989777

CREATE PROCEDURE ComparTables AS

declare @SQL varchar(8000)
   , @T1ColumnList varchar(1000)
   , @T2ColumnList varchar(1000)
   , @employee3    Varchar (100)


Set @t1ColumnList = 'employeeID,fullname, Salary'
Set @t2Columnlist = 'employeeID,fullname, Salary'


IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList

set @SQL = 'DECLARE @Employee2 TABLE
 (
  EmployeeID  int(4),
  Fullname     varchar(32),
  Salary         int (4)
  )

SELECT ''' + @employee2 + ''' AS TableName, ' + @t1ColumnList +

 ' FROM @employee2  UNION ALL SELECT ''' + @employee3 + ''' As TableName, ' +

 @t2ColumnList + ' FROM ' + @employee3

set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +

 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +

 ' HAVING COUNT(*) = 1'

exec ( @SQL)
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16991335
you are trying to use @employee2 in two different ways in theis code:

set @SQL = 'DECLARE @Employee2 TABLE
 (
  EmployeeID  int(4),
  Fullname     varchar(32),
  Salary         int (4)
  )

here you are declaring @employye2 as a Table type variable

SELECT '' + @employee2 + '' AS TableName, ' + @t1ColumnList +


and here, you appear to be trying to get data from a FILED, whose name is defined as varaible (but this CANNOT be the Table type variable that you declared above.  
 ' FROM  + @employee2 +  UNION ALL SELECT ''' + @employee3 + ''' As TableName, ' +

 @t2ColumnList + ' FROM ' + @employee3

set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +

 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +

 ' HAVING COUNT(*) = 1'

exec ( @SQL)

Can you expleain, in English, what is the business situation that you are trying to achieve with this Stored Procedure?

AW
0
 

Author Comment

by:Aopoku
ID: 16992178
Just comparing the two tables for changes in employee2 table.  Updates, Inserts and deletes.  employee2 happens to be a variable table.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16997423
what do you mean by 'variable' table?

AW
0
 

Author Comment

by:Aopoku
ID: 17094682
angelIII.
per your comment earlier
how can I achieve the same thing without the dynamic SQL?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What we learned in Webroot's webinar on multi-vector protection.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

809 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