Solved

Variable Tables in Stored Procedures

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now