Monday, March 12, 2012

How to set the default value

I want to use SQL to set the default of a field.
How can I implement with DDL?ad
CREATE TABLE #Test
(
col1 INT NOT NULL PRIMARY KEY,
col2 DATETIME DEAFULT GETDATE()
)
INSERT INTO #Test (col1) VALUES (1)
GO
SELECT * FROM #Test
"ad" <ad@.wfes.tcc.edu.tw> wrote in message
news:OZmeP74XFHA.3584@.TK2MSFTNGP14.phx.gbl...
> I want to use SQL to set the default of a field.
> How can I implement with DDL?
>|||CREATE TABLE Testdata
(
TestIDCOlumn INT,
TestColumn varchar(10) DEFAULT 'Test'
)
Insert into Testdata(TestIDColumn) Values (1)
Select * from Testdata
Drop Table Testdata
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"ad" <ad@.wfes.tcc.edu.tw> schrieb im Newsbeitrag
news:OZmeP74XFHA.3584@.TK2MSFTNGP14.phx.gbl...
>I want to use SQL to set the default of a field.
> How can I implement with DDL?
>|||Thank,
If the TestColumn have create before, but did not set default value yet.
Can I modify the structrue now , set the default value to that column?
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> gl
sD:ew5otC5XFHA.2076@.TK2MSFTNGP15.phx.gbl...
> CREATE TABLE Testdata
> (
> TestIDCOlumn INT,
> TestColumn varchar(10) DEFAULT 'Test'
> )
> Insert into Testdata(TestIDColumn) Values (1)
> Select * from Testdata
> Drop Table Testdata
> --
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
> "ad" <ad@.wfes.tcc.edu.tw> schrieb im Newsbeitrag
> news:OZmeP74XFHA.3584@.TK2MSFTNGP14.phx.gbl...
>|||alter table TestData
add constraint DF_TestDate_TestColumn
default ('Test') for TestColumn
HTH,
Gert-Jan
ad wrote:[vbcol=seagreen]
> Thank,
> If the TestColumn have create before, but did not set default value yet.
> Can I modify the structrue now , set the default value to that column?
> "Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> g
l
> sD:ew5otC5XFHA.2076@.TK2MSFTNGP15.phx.gbl...|||You would probably have to
rename the column
add a new column with default value
move data from orig column to new column
drop orig column
rename new column to orig name
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"ad" <ad@.wfes.tcc.edu.tw> wrote in message
news:%23BW3EA%23XFHA.4036@.tk2msftngp13.phx.gbl...
> Thank,
> If the TestColumn have create before, but did not set default value yet.
> Can I modify the structrue now , set the default value to that column?
> "Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de>
> gl
> sD:ew5otC5XFHA.2076@.TK2MSFTNGP15.phx.gbl...
>

No comments:

Post a Comment