How to insert into the tempo table twice

I picked up some SQL similar to the following:

IF EXISTS(SELECT name FROM tempdb..sysobjects WHERE name Like N'#tmp%'
 and id=object_id('tempdb..#tmp'))
DROP TABLE #tmp

into #tmp
select * from permTable

I need to add more data to #tmp before continuing with the processing:

insert into #tmp
select * from permTable2

But this gives errors because SQL suggested sizes and types for #tmp columns (for example, if permTable has a column full of ints, but permTable2 has a column with the same name but with NULL in one record, you get "Cannot insert a NULL value in column "IsPremium", table "tempdb.dbo. # tmp").

How do I get #tmp to have the types I want? Is this really bad practice?

+3
source share
2 answers

Have you considered creating a var table? You can declare columns like

declare @sometable table(
     SomeField [nvarchar](15),
     SomeOtherField [decimal](15,2));
+5

- . create table, insert.

+1

Source: https://habr.com/ru/post/1770555/


All Articles