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
Solved

Variable Tables in Stored Procedures

Posted on 2006-06-26
10
350 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 143

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

860 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