Solved

Is it possible to find the structure of a Global Table which is created in Proc A and referenced in Proc B

Posted on 2008-10-20
5
210 Views
Last Modified: 2012-05-05
I have a global table created in proc A and then referenced in proc b.

I would like to know in Proc B, if the global table has a particular column, how can I do that
Proc A
INSERT INTO #Activity
EXEC dbdetails.dbo.Proc_B
    @TableName = @GlobalTableName_parm, 
    @ServerName = @@SERVERNAME,
    @UpdateFlag = 1,
    @SuccessFlag = @SuccessFlag OUTPUT
 
Proc_B
CREATE PROCEDURE Proc_B
    @TableName  varchar(100),
    @ServerName varchar(50),
    @UpdateFlag int,
    @SuccessFlag tinyint OUTPUT
AS
 
I would like to know the sql that I can excute to verify that @TableName  has a particular column say 'Test'

Open in new window

0
Comment
Question by:countrymeister
[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
5 Comments
 
LVL 4

Expert Comment

by:randy_knight
ID: 22762570
What do you mean by global table?  A global temporary table (i.e. ##table)?
0
 
LVL 1

Author Comment

by:countrymeister
ID: 22762599
Yes , the global table is named ##TableA_spid
0
 
LVL 4

Expert Comment

by:randy_knight
ID: 22762775
I don't think there is an easy way to do that.  However, an altenative would be to create your global temp tables as hard tables in tempdb (i.e. tempdb.dbo.TableA_spid) and then drop them at the end of your process.  

Then you could used this to find the column:



if exists(select * from tempdb.dbo.syscolumns where id = object_id('TableA_spid' and name = 'test')
    print 'column exists'
else
    print 'column does not exist'

Open in new window

0
 
LVL 1

Author Comment

by:countrymeister
ID: 22762916
I am creating the global table as follows
SELECT @GlobalTableName = '##TableA_' + convert(varchar(10),@@spid)

SELECT @EXECSQLText = N'CREATE TABLE ' + @GlobalTableName
( col1, col2....)

I am droping them after I use them

But the following sql does not work to check if the column exists in the global table, where @TableName is the global table

Set @IncludeTaxes = 0
-- Check if the calling proc has passed the test column  to the  proc B
Set @IncludeTaxes = (SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND COLUMN_NAME = 'Test')
0
 
LVL 4

Accepted Solution

by:
randy_knight earned 500 total points
ID: 22763480
Right.  Because INFORMATION_SCHEMA.COLUMNS won't have info on temporary tables.  What I am saying is create a "permanent" table in tempdb like this:

SELECT @GlobalTableName = 'TableA_' + convert(varchar(10),@@spid)

Then it will be in syscolumsn and the DSV above.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

636 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