?
Solved

Variable Tables in Stored Procedures

Posted on 2006-06-26
10
Medium Priority
?
357 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
[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
  • 3
  • 2
  • 2
  • +1
10 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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