Lee posted on July 11, 2011 13:55

I’ve added a beginner category to my blog, so here’s the first post. Hope it helps someone out along the way…

SELECT INTO moves a certain number of things to the newly created table from the source table; some things it doesn’t move, and these are well known. NULL value specifications for a given column get moved, but what if you don’t want the NULL? The following example illustrates:

USE _;

GO

IF OBJECT_ID('testTable') IS NOT NULL

       DROP TABLE testTable

GO

SELECT DISTINCT StateProvinceCode

INTO testTable

FROM AdventureWorksDW2008R2.dbo.DimGeography

GO

ALTER TABLE testTable

       ADD CONSTRAINT pk_testTable PRIMARY KEY (StateProvinceCode)

GO

Msg 8111, Level 16, State 1, Line 1

Cannot define PRIMARY KEY constraint on nullable column in table 'testTable'.

Msg 1750, Level 16, State 0, Line 1

Could not create constraint. See previous errors.

 

How about ISNULL or COALESCE?

SELECT DISTINCT ISNULL(StateProvinceCode,'î') AS StateProvinceCode

INTO testTable

FROM AdventureWorksDW2008R2.dbo.DimGeography

WHERE StateProvinceCode!='î'

GO

 

 


 

 

 

Add some weird character or combination of characters that fall outside of the set of values from your SELECT list so that you know you won’t miss a record for your new column.

 

 

Thanks,

Lee

 

 

-----------------------------

 

 


Posted in: Beginner  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Info

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog