Solved

Microsoft SQL 2005 SQL Statement Error

Posted on 2008-10-24
2
237 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

691 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