About 961,000 results
Open links in new tab
  1. If table exists with dynamic query in SQL Server

    Aug 7, 2014 · Here is the checking whether table exists or not: IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N' + @TABLENAME + ') AND type in (N'U')) table sys.objects contains description all objects in a database. Function OBJECT_ID() - return id of object by its name. type in (N'U')) - checks that object was created by user.

  2. I want to create/drop a dynamically-named temp table. Is there

    Jun 7, 2024 · If you want to use dynamic SQL, you'd need to use sp_executesql. If you're the only one with access to the code and control over the input in the TEMP_TABLE_NAME variable, then you can get away with EXEC. Example using OP's case DECLARE @SQL VARCHAR(500); SELECT @SQL = 'DROP TABLE IF EXISTS ' + @TEMP_TABLE_NAME + ';'; EXEC(@SQL);

  3. SQL Server DROP TABLE IF EXISTS Examples

    Dec 18, 2024 · Learn various ways to check if a SQL Server table exists before trying to drop the table to avoid the table does not exist error.

  4. Dynamically creating a SQL Server table with dynamic SQL

    Aug 8, 2022 · In this post, we are going to look at how we can use dynamic SQL to build a table programmatically which you can then use in your scripts. We are going to make use of the Information Schema, this is specific to each database, in this example, all of the table we want to target are within the same StackOverflow database.

  5. Check for Existence Of Column And Update If Exists Using Dynamic SQL

    Feb 21, 2012 · SET @Source_Table = ‘Source_Table’ declare @SQL varchar(8000) set @SQL=’ ’ set @SQL='IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = ’ + QUOTENAME(@Source_Column, CHAR(39)) + ’ AND TABLE_NAME = ’ + QUOTENAME(@Source_Table, CHAR(39)) + ‘) BEGIN UPDATE tt SET [desc] = MI.[desc] FROM Target_table tt

  6. Dynamically creating a @table Table from a current table - SQLServerCentral

    Sep 27, 2016 · Create the table in main stored proc, then ALTER it using dynamic SQL. Since the temp table already exists, the dynamic SQL is basically a sub-proc of the main proc, and it can "see"...

  7. sql server - Creating temporary table dynamically using condition ...

    Mar 10, 2019 · Use a global temporary table instead of a session-scoped one (##table instead of #table). This will allow you to create the table conditionally using dynamic SQL and then access it in the original session.

  8. Creating a table using Dynamic SQL in stored procedure get

    Apr 5, 2004 · This is how to do the above: CREATE OR REPLACE PROCEDURE SCP_TESTA as table_already_exists exception; pragma exception_init ( table_already_exists, -955 ); BEGIN for x in (select stage_table, product_category, product_attribute from pcm) loop begin execute immediate 'create table stg_'||x.stage_table || ' as select sysdate dummy_date fro...

  9. Dynamically Creating a Table in SQL Server from Information …

    Jul 19, 2024 · This SQL script dynamically creates a table in SQL Server by utilizing INFORMATION_SCHEMA and system views. It constructs the table definition, including columns, data types, constraints, and default values.

  10. sql server - SQL "if exists..." dynamic query - Stack Overflow

    You can use EXEC to execute sql statement, then call @@ROWCOUNT which Returns the number of rows affected by the last statement, to check row exists in sql select stetement.

Refresh