Solved

Variable Tables in Stored Procedures

Posted on 2006-06-26
10
349 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
10 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 142

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

785 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