April 22, 2016 at 9:33 am
What is wrong with this Statement?
ALTER TABLE tblCall
ADD CONSTRAINT DF_ReinspectTag
DEFAULT (RAND) FOR ReinspectTag
Msg 128, Level 15, State 1, Line 219
The name "RAND" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
April 22, 2016 at 9:38 am
RAND() is a function. You need those parentheses.
Also note that if you create such a column and insert multiple rows in the same statement, they will all have the same "random" value.
Cheers!
April 22, 2016 at 9:38 am
April 22, 2016 at 9:39 am
Jacob Wilkins (4/22/2016)
RAND() is a function. You need those parentheses.Also note that if you create such a column and insert multiple rows in the same statement, they will all have the same "random" value.
Cheers!
Bugger.
Beat my by 29 seconds.
April 22, 2016 at 10:19 am
MadAdmin (4/22/2016)
guessing that it should be(rand())
Will check ina bit.
Thanks it worked.
Does this look right?
ALTER TABLE tblCall
ADD CONSTRAINT DF_ReinspectTag
DEFAULT (rand()) FOR ReinspectTag
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
April 22, 2016 at 12:44 pm
multi row inserts will insert the same default value form RAND() for all rows in the insert statement.
is that ok? (oops! tip of the hat to Jacob, who noticed the same logical error first!)
create table #example(exampleid int identity(1,1) not null primary key,
sometext varchar(128),
someRand float default rand() )
insert into #example(sometext)
SELECT top 5 name from sys.tables
select * from #example
Lowell
April 22, 2016 at 1:09 pm
create table #example(exampleid int identity(1,1) not null primary key,
sometext varchar(128),
someRand int default Abs(Checksum(Newid()) % 90000000 ))
insert into #example(sometext)
SELECT top 5 name from sys.tables
select * from #example
drop table #example
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
April 22, 2016 at 5:43 pm
Welsh Corgi (4/22/2016)
MadAdmin (4/22/2016)
guessing that it should be(rand())
Will check ina bit.
Thanks it worked.
Does this look right?
ALTER TABLE tblCall
ADD CONSTRAINT DF_ReinspectTag
DEFAULT (rand()) FOR ReinspectTag
It works but no... it doesn't look right. Why would you be using a random value for a "ReinspectTag"???
--Jeff Moden
Change is inevitable... Change for the better is not.
April 25, 2016 at 3:22 am
Jeff Moden (4/22/2016)
Welsh Corgi (4/22/2016)
MadAdmin (4/22/2016)
guessing that it should be(rand())
Will check ina bit.
Thanks it worked.
Does this look right?
ALTER TABLE tblCall
ADD CONSTRAINT DF_ReinspectTag
DEFAULT (rand()) FOR ReinspectTag
It works but no... it doesn't look right. Why would you be using a random value for a "ReinspectTag"???
And, moreover, a random value between 0 and 1 that won't be unique.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 25, 2016 at 9:20 am
An interesting method I've used before is to use NEWID() in ORDER BY.
In this example I select the 6 winning numbers of the next lotto draw!
WITH MyNumbers (n) AS
(
--Only 49 possible numbers on lotto ticket!
SELECT TOP 49 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
)
SELECT TOP 6 n FROM MyNumbers ORDER BY NEWID()
This way you won't get repeated values if that is a requirement.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply