Solved

Microsoft SQL 2005 SQL Statement Error

Posted on 2008-10-24
2
235 Views
Last Modified: 2010-03-19
I am using below statement to add an new column into the table. The satement is running well, but showing null value instead of default value '0'. Please help

Select @SQL= 'ALTER TABLE #Row ADD ' + QUOTENAME(@ColumnText) + ' Numeric(18,3) Default 0'
Exec   (@SQL)
0
Comment
Question by:Mehram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 22794948
That is because simply adding the column with a default value does not necessarily insert zero values
into the existing records--the default kicks in when new records are added.

You need to follow up with an update:

UPDATE TableName SET ColumnName = 0 WHERE ColumnName IS NULL
0
 
LVL 1

Assisted Solution

by:isdi
isdi earned 250 total points
ID: 22795060
Mathhew's answer is correct. However, just to clarify ...

In your case it would look like:

Select @SQL= 'ALTER TABLE #Row ADD ' + QUOTENAME(@ColumnText) + ' Numeric(18,3) Default 0'
Exec   (@SQL)
GO

SELECT @SQL= 'UPDATE #Row SET ' + @ColumnText + ' = 0 WHERE ' + @ColumnText IS NULL'
GO

This is assuming that #Row and @ColumnText are still in scope.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question