SQL Server: multiple inserts in temp table when creating a value for each other insert

Forgive my code, I'm still learning.

I want to make several select statements and paste them into one temporary table. For each select statement, I would like some kind of identification to know from which selection statement the data comes from.

The following is an unobvious idea to use the case example below.

Error:

Msg 8152, Level 16, State 14, Line 14
String or binary data will be truncated.

the code:

if object_id('tempdb..#PP') is not null 
     drop table #PP

SELECT 
    #Pr.*, 
    Case
       When TranID = TranID then 'BK Problem'
       Else 'Error' End as 'Test'
INTO #PP
FROM #Pr
WHERE TypeID = 't'

--CCA Test
INSERT INTO #PP
    SELECT 
        #Pr.*, 
        Case
           When TranID = TranID then 'CCA Problem'
           Else 'Error' End as 'Test'
    FROM #Pr
    WHERE TypeID = 'r'

I appreciate any help pointing me in the right direction.

thank

+4
source share
2 answers

SELECT.. INTO.., , .

, "BK Problem", Test "BK Problem" 10.
( , : PRINT LEN('BK Problem'))

" CCA" Test , 11 .

, :

SELECT #Pr.*, CAST('BK Problem' as VARCHAR(11)) Test
INTO #PP
FROM #Pr
WHERE TypeID = 't'

--CCA Test
INSERT INTO #PP
SELECT #Pr.*, 'CCA Problem'
FROM #Pr
WHERE TypeID = 'r'
+2

. , select, "Test" varchar (10). 11 . cast/convert case, .

SELECT #Pr.*, 
       convert(varchar(20), Case
       When TranID = TranID then 'BK Problem'
       Else 'Error' End) as 'Test'
INTO #PP
FROM #Pr
WHERE TypeID = 't'
+2

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


All Articles