create table t(id int ,code numeric(10,2))
insert t
select 1,'1.57'
union
select 2,' 1.78'
union
select 3,'2.4'
union
select 4,'-1.5'
union
select 5,'0.8'
union
select 6,'1.57'
union
select 7,'1.4'
go
create proc p
@code numeric(10,2)
as
select *,abs(code-cast('1.5' as numeric(10,2))) as pabs into tt from t
select top 1 id,code from tt where pabs=(select min(pabs) from tt) order by newid()
drop table tt
go
exec p '1.5'drop proc p
drop table t
insert t
select 1,'1.57'
union
select 2,' 1.78'
union
select 3,'2.4'
union
select 4,'-1.5'
union
select 5,'0.8'
union
select 6,'1.57'
union
select 7,'1.4'
go
create proc p
@code numeric(10,2)
as
select *,abs(code-cast('1.5' as numeric(10,2))) as pabs into tt from t
select top 1 id,code from tt where pabs=(select min(pabs) from tt) order by newid()
drop table tt
go
exec p '1.5'drop proc p
drop table t
select top 1 id from abc
where FLOOR(b*10)/10=1.5
order by newid()
返回小于或等于所给数字表达式的最大整数。语法
FLOOR ( numeric_expression ) 参数
numeric_expression精确数字或近似数字数据类型类别的表达式(bit 数据类型除外)。 返回类型
返回与 numeric_expression 相同的类型。示例
此示例说明正数、负数和货币值在 FLOOR 函数中的运用。SELECT FLOOR(123.45), FLOOR(-123.45), FLOOR($123.45)结果为与 numeric_expression 数据类型相同的计算值的整数部分。 --------- --------- -----------
123 -124 123.0000
创建 uniqueidentifier 类型的唯一值。 语法
NEWID ( )返回类型
uniqueidentifier示例
A.对变量使用 NEWID 函数
下面的示例使用 NEWID 对声明为 uniqueidentifier 数据类型的变量赋值。在测试该值前,将先打印 uniqueidentifier 数据类型变量的值。-- Creating a local variable with DECLARE/SET syntax.
DECLARE @myid uniqueidentifier
SET @myid = NEWID()
PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)下面是结果集:Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF说明 对于每台计算机,由 NEWID 返回的值不同。所显示的数字仅起解释说明的作用。
B.在 CREATE TABLE 语句中使用 NEWID
下面的示例创建具有 uniqueidentifier 数据类型的 cust 表,并使用 NEWID 将默认值填充到表中。为 NEWID() 赋默认值时,每个新行和现有行均具有 cust_id 列的唯一值。 -- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
cust_id uniqueidentifier NOT NULL
DEFAULT newid(),
company varchar(30) NOT NULL,
contact_name varchar(60) NOT NULL,
address varchar(30) NOT NULL,
city varchar(30) NOT NULL,
state_province varchar(10) NULL,
postal_code varchar(10) NOT NULL,
country varchar(20) NOT NULL,
telephone varchar(15) NOT NULL,
fax varchar(15) NULL
)
GO
-- Inserting data into cust table.
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
'90110', 'Finland', '981-443655', '981-443655')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
'08737-363', 'Brazil', '(14) 555-8122', '')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,
'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
'8010', 'Austria', '7675-3425', '7675-3426')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')
GOC. 使用 uniqueidentifier 和变量赋值
下面的示例声明局部变量 @myid 为 uniqueidentifier 数据类型。然后使用 SET 语句为该变量赋值。DECLARE @myid uniqueidentifier
SET @myid = 'A972C577-DFB0-064E-1189-0154C99310DAAC12'
GO
drop table abcgocreate table abc(id int not null identity(1,1),b numeric(15,2),primary key(id))
go---------測試一---------
insert into abc
select 1.57 union all
select 1.78 union all
select 2.4 union all
select -1.5 union all
select 0.8 union all
select 1.57 union all
select 1.4
goselect * from abc
/*
id b
1 1.57
2 1.78
3 2.40
4 -1.50
5 .80
6 1.57
7 1.40*/select * from abc where id in
(
select id from
(
select top 1 id,b,abs(b-1.5) cy from abc order by cy,newid()
)a
)
/*
id b
6 1.57*/
select top 1 id from abc
where FLOOR(b*10)/10=1.5
order by newid()这个好像不是很完善呀.有时会取不到数据的.如:select top 1 id from abc where FLOOR(b*10)/10=3.0 order by newid()时就没有值..如果不相等就要取到最接近....
不过还是谢谢各位的帮忙.....