Solved

Microsoft SQL 2005 SQL Statement Error

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

732 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