Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

stored procedure---finding all tables that contain a specific column Expand / Collapse
Author
Message
Posted Saturday, November 29, 2008 9:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
Hello, I am working on a stored procedure that will accept a column name and returns the name of the table or tables that have the column by that name. I am also coding a statement that calls the procedure. \

Below is what I have so far but when I call the procedure I get the following error when using the column name VendorID:
Incorrect syntax near 'VendorID'.

--Creating stored procedure called sp_WhichTable
use AP
GO

CREATE PROC sp_WhichTable
AS

SELECT o.name as [Table], c.name as [Column]
FROM SYSOBJECTS o JOIN SYSCOLUMNS c ON o.id = c.id
WHERE c.name like '%order%' AND o.xtype = 'u'
ORDER BY 1


--Calling the stored procedure
Declare @ColumnName varchar
exec sp_WhichTable @ColumnName = 'VendorID'
Post #610723
Posted Saturday, November 29, 2008 9:38 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 39,960, Visits: 36,315
Well, your procedure (as you posted it) doesn't have any input parameters, hence trying to pass one is going to give an error.

You don't need a variable called @ColumnName, you need to change the procedure so that it accepts an input parameter, of type varchar, called @ColumnName



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #610726
Posted Saturday, November 29, 2008 11:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
ok, I added an imput parameter and removed the variable. I am still getting this error message when calling the procedure- Incorrect syntax near 'VendorID'.

CREATE PROC sp_WhichTable
@ColumnName varchar(50)
AS

SELECT o.name as [Table], c.name as [Column]
FROM SYSOBJECTS o JOIN SYSCOLUMNS c ON o.id = c.id
WHERE c.name like '%order%' AND o.xtype = 'u'
ORDER BY 1


exec sp_WhichTable @ColumnName 'VendorID'
Post #610750
Posted Saturday, November 29, 2008 12:13 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 39,960, Visits: 36,315
You left out the equals sign in the procedure call. You had that part right last time.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #610753
Posted Saturday, November 29, 2008 2:52 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
--The procedure was created ok:

CREATE PROC sp_WhichTable
@ColumnName varchar(50)
AS

SELECT o.name as [Table], c.name as [Column]
FROM SYSOBJECTS o JOIN SYSCOLUMNS c ON o.id = c.id
WHERE c.name like '%order%' AND o.xtype = 'u'
ORDER BY 1

--When I execute the sp :

exec sp_WhichTable @ColumnName = 'invoiceID'

I get 2 blank columns with the column headers TABLE and COLUMN. I expected to get some results.
Looking at the tables I see that the column InvoiceID is in the InvoicesTable and InvoicesLineItems Tables.

Post #610784
Posted Saturday, November 29, 2008 3:15 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 39,960, Visits: 36,315
You're passing the value invoiceid to the procedure, but you're doing nothing with it within the procedure.

If you want to use the parameter to filter the query, it needs to be referenced within the query somehow. Perhaps in place of the hardcoded 'order'?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #610790
Posted Saturday, November 29, 2008 3:23 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 39,960, Visits: 36,315
Two more things.

Stored procedures should not be created with the sp_ name, That's for system procedures and means that SQL will look in one of the system databases first for the proc.

Since you're on 2005, use sys.tables and sys.columns instead of sysobjects and syscolumns. They are just there for backward compatibility and will be removed in a future version.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #610792
Posted Saturday, November 29, 2008 4:22 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
--The procedure was created ok:

CREATE PROC sp_WhichTable
@ColumnName varchar(50)
AS

SELECT o.name as [Table], c.name as [Column]
FROM SYSOBJECTS o JOIN SYSCOLUMNS c ON o.id = c.id
WHERE c.name like '%order%' AND o.xtype = 'u'
ORDER BY 1

--When I execute the sp :

exec sp_WhichTable @ColumnName = 'invoiceID'

I get 2 blank columns with the column headers TABLE and COLUMN. I expected to get some results.
Looking at the tables I see that the column InvoiceID is in the InvoicesTable and InvoicesLineItems Tables.

Post #610798
Posted Saturday, November 29, 2008 4:33 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
I am now referencing the parameter in the stored procedure as follows:

use master
GO

CREATE PROC spWhichTable
@ColumnName varchar(50)
AS

SELECT o.name as [Table], c.name as [Column]
FROM sys.tables o JOIN sys.columns c ON o.id = c.id
WHERE c.name = '@ColumnName' AND o.xtype = 'u'

I am receiving these errors:

Msg 207, Level 16, State 1, Procedure spWhichTable, Line 7
Invalid column name 'id'.
Msg 207, Level 16, State 1, Procedure spWhichTable, Line 7
Invalid column name 'id'.
Msg 207, Level 16, State 1, Procedure spWhichTable, Line 8
Invalid column name 'xtype'.




Post #610800
Posted Saturday, November 29, 2008 4:43 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
try it like this:
CREATE PROC spWhichTable
@ColumnName varchar(50)
AS

SELECT o.name as [Table]
, c.name as [Column]
FROM sys.tables o
JOIN sys.columns c ON o.object_id = c.object_id
WHERE c.name = '@ColumnName'
AND o.type = 'U'



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #610802
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse