Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:42:45+00:00 2026-06-04T15:42:45+00:00

I have written a stored procedure where a table name and database name collect

  • 0

I have written a stored procedure where a table name and database name collect
using cursor from different two tables.
But my problem is when I run a query to find out a table exists in a database or not, then show a error.
Now how can I run the query and store output into a variable?

Declare @table_exist nvarchar(200),@val1 nvarchar(200),@return1 nvarchar(200);
  SET @table_exist= 
    'SELECT 1 FROM '+@db_name+'.sys.tables 
    where name='+@table_name+'';
  EXEC sp_executesql @table_exist,@return1 OUTPUT;  
  select @return1;  

ERROR:

Invalid column name 'table name'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:42:46+00:00Added an answer on June 4, 2026 at 3:42 pm

    You should use quotename when building dynamic query:

       SET @table_exist= 
         'SELECT 1 FROM '+ quotename(@db_name)+'.sys.tables 
         where name='+quotename(@table_name)+'';
    

    When facing such an error the best would be to print @table_exists and see what is actually built.

    I haven’t looked properly at your query. You are missing apostrophes:

       SET @table_exist= 
         'SELECT 1 FROM '+ quotename(@db_name)+'.sys.tables 
         where name=''' + @table_name +'''';
    

    UPDATE:

    When using output variable you should set it in a query:

      SET @table_exist= 
        'SELECT @return1 = 1 FROM ' + quotename(@db_name) + '.sys.tables 
        where name='''+@table_name+'''';
    

    To prevent result set from returning to client, create temporary table and insert result set into it. In this case this will leave only one result set, the result of select @return1:

      declare @tbl table (exist bit)
      insert into @tbl
      EXEC sp_executesql @table_exist, N'@return1 nvarchar(200) out', @return1 OUT;
      select @return1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written stored procedure to poll the data from the database in biztalk.but
I have written a Stored procedure which return some fields from a temporary table
I have written a single stored procedure that returns 2 tables: select * from
Scenario I have a stored procedure written in T-Sql using SQL Server 2005. SEL_ValuesByAssetName
I have written a package that has a stored procedure and a REF cursor.
I am using oracle 11g and have written a stored procedure which stores values
I have written the below procedure but when it executes the table always keep
I have a stored procedure written in T-SQL and want to use a table
I have written below stored procedure. In which i need to create Two Temporary
I have written a stored procedure that, yesterday, typically completed in under a second.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.