stoner
stoner

Reputation: 407

Add column with default value 0.0

I have a table in sql server, but i want to add a extra column full of zeros i do this :

DECLARE @var numeric(10,1) set @var = 0.0
 SELECT tmp.a,
        tmp.b,
        @var
 INTO #tmp2
 from #tmp1 as tmp

when i select * from #tmp2 the column get 0 but i want 0.0

Upvotes: 0

Views: 740

Answers (2)

Jayesh Prakash
Jayesh Prakash

Reputation: 141

First of all your SQL is incorrect you need to give a column name to @var in SELECT list. Secondly, I was not able to reproduce this problem. I am using SQL Server 2014 and it returns 0.0 from #tmp2 and not 0.

Upvotes: 0

t-clausen.dk
t-clausen.dk

Reputation: 44356

You need to name the columns, this will work as intended leaving the column C with the values 0.0:

CREATE TABLE #temp1(a int, b int)
INSERT #temp1 values(1,1),(2,2)

DECLARE @var numeric(10,1) set @var = 0.0
 SELECT a,
        b,
        @var c
 INTO #tmp2
 FROM #temp1

 SELECT * from #tmp2

Result:

a  b  c
1  1  0.0
2  2  0.0

Upvotes: 2

Related Questions