SQL error Invalid column name

I have the following stored procedure

CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here


exec ('Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG ='+ @TableName)
END

Which when I execute using the following

EXEC @return_value = [GEN].[ColumnsList]
@DatabaseName = N'SSO',
@TableName = N'SSO_Config'

I get the following error

Msg 207, Level 16, State 1, Line 1
Invalid column name 'SSO_Config'.
(1 row(s) affected)

I am not sure how to overcome this error.

can anybody help?

I am using SQL 2005
Mr_ShawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BrandonGalderisiCommented:
The problem is happening because you don't have the value in quotes.  But you don't need to use dynamic SQL
Try:
 
CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
 
    -- Insert statements for procedure here
 
 
exec ('Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG ='''+ @TableName + '''')
END
 
 
 
OR
 
 
CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
 
    -- Insert statements for procedure here
 
 
Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG =@TableName
END

Open in new window

0
Aneesh RetnakaranDatabase AdministratorCommented:
exec ('Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG ='''+ @TableName+'''')
0
Aneesh RetnakaranDatabase AdministratorCommented:
are u sure about this  TABLE_CATALOG =@TableName
should n't it be TABLE_NAME = @TableName
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

BrandonGalderisiCommented:
Or:



CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
 
    -- Insert statements for procedure here
 
 
exec ('Select COLUMN_NAME, DATA_TYPE from [' + @databasename + '] .INFORMATION_SCHEMA.COLUMNS where TABLE_NAME ='''+ @TableName + '''')
END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris LuttrellSenior Database ArchitectCommented:
The problem is that we are not sure of your desired usage of @DatabaseName, if it is truely the database then Brandon is right above.  If you are thinking Database is a specific Schema within the Ardentia_Healthware_5_Release database then try the code below.
CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
 
    -- Insert statements for procedure here
 
 
Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = @DatabaseName AND TABLE_CATALOG = @TableName
END

Open in new window

0
Chris LuttrellSenior Database ArchitectCommented:
Sorry, I copied from the wrong place and did not change TABLE_CATALOG to TABLE_NAME.
CREATE PROCEDURE [GEN].[ColumnsList]
      -- Add the parameters for the stored procedure here
(
@DatabaseName nvarchar(255),
@TableName nvarchar(255)
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
 
    -- Insert statements for procedure here
 
 
Select COLUMN_NAME, DATA_TYPE from Ardentia_Healthware_5_Release.INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = @DatabaseName AND TABLE_NAME = @TableName
END

Open in new window

0
Mr_ShawAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.