wullxz
wullxz

Reputation: 19430

Turn SQL Server table variable into real table

I have a temporary table created with:

DECLARE @tbl_fk_errors TABLE (
    id int Primary Key IDENTITY(1,1)
    , tbl_id int
    , tbl_name nvarchar(128)
    , tbl_record_id int
    , tbl_field nvarchar(20)
    , tbl_field_value nvarchar(20)
    , jnktn_field_value nvarchar(20)
    , jnktn_field nvarchar(20)
    , jnktn_tblname nvarchar(128)

That table will be filled in my script. Now I want to output that table into a file and found xp_cmdshell which can do that. But that stored procedure can't take my table var because it opens a new connection to the DB (because of the "bcp").

I need to temporary save that table into my DB (and drop it after, but that's no big deal then).

How can I quickly save a table stored in a table variable as real table in my DB?

Upvotes: 1

Views: 236

Answers (1)

usr
usr

Reputation: 171178

select *
into table --destination
from @table --source

As a bonus, wrap a transaction around this to make sure you don't leave a "dangling" table in case of an error. DDL is transactional in SQL Server. On rollback, the table will be removed.

Upvotes: 2

Related Questions