?
Solved

Microsoft SQL 2005 SQL Statement Error

Posted on 2008-10-24
2
Medium Priority
?
238 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 93

Accepted Solution

by:
Patrick Matthews earned 1000 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 1000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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