I have started to test this in Sybase before you added the sql-server tag so I will provide both, the first one explain the idea, the second will prove this is possible in both DBMS.
Note that the solution is working but I would not use this as a long term solution. I didn't research on the possibilities of this to be a documented behavior, and could fail in the future (or already, based on my database version)
Sybase
Just use a convert
function on a null
value to add the empty columns.
select 'a' as foo,
convert(varchar(25), null) as serial_no
into #temp
Then to test it, I insert a long value :
insert into #temp values ('b', 'abcdefghijklmnopqrstuvwxyz')
And the result :
select * from #temp
foo,serial_no
'a',
'b','abcdefghijklmnopqrstuvwxy'
The 26th character is missing, we have a varchar(25)
SQL-Server
The same code will provide an error due to the truncated value
select 'a' as foo, convert(varchar(25), null) as serial_no
into temp_foo
insert into temp_foo values ('b', 'abcdefghijklmnopqrstuvwxyz')
insert into temp_foo values ('c', 'abcdefghijklmnopqrstuvwxy')
select * from temp_foo
The line with b
provide a value to long and gives me :
Msg 8152, Level 16, State 14, Line 4
String or binary data would be truncated.
The statement has been terminated.
But the line with c
with 25 character fits.
foo,serial_no
'a',
'c','abcdefghijklmnopqrstuvwxy'
SELECT
cust_ac_no, ord_no, ref_no, net_svc_id, job_type, ord_status, ord_status_date, ord_crt_date,
convert(varchar(25), null) as serial_no,
convert(varchar(25), null) as msisdn,
convert(varchar(25), null) as imsi,
convert(varchar(25), null) as bts_id,
INTO tempdb..xtiankwiksetreport
FROM wo_order
WHERE job_type IN ('EXTR', 'EXTC')
AND svc_type='4G'
AND ref_no=2
AND ord_status IN ('PL', 'JL')