22,209
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE tb(id int,val int,num numeric(10,2));
INSERT tb(id,val) VALUES(1,10);
INSERT tb(id,val,num) VALUES(2,20,3.23)
GO
CREATE VIEW v_Random
AS
SELECT random = NEWID()
GO
CREATE FUNCTION dbo.GetRandom()
RETURNS numeric(10,2)
AS
BEGIN
RETURN (SELECT (ABS(CHECKSUM(random)) % ((329 - 310 + 1) ) + 310) / 100.
FROM v_Random)
END
GO
ALTER TABLE tb ADD CONSTRAINT DF_num DEFAULT dbo.GetRandom() FOR num;
GO
INSERT tb(id,val) VALUES(3,30);
INSERT tb(id,val) VALUES(4,40);
SELECT * FROM tb;
GO
DROP TABLE tb;
DROP FUNCTION dbo.GetRandom;
DROP VIEW v_Random
if object_id('tb')is not null drop table tb
go
create table tb(ID int,num smallint default CAST(abs(checksum(newid())%500) AS SMALLINT))
insert tb(ID) select 1
insert tb(ID) select 2
select * from tb
/*
ID num
----------- ------
1 185
2 323
*/
create table test(name varchar(20),value as cast(floor(rand()*100) as int))
go
insert test(name) values('test')
select * from test
/**
name value
-------------------- -----------
test 46
(1 行受影响)
**/
drop table test