搜集论坛SQL问题集合.仅供自己和有兴趣的朋友参考。不做其他用途

来源:互联网 发布:win7添加网络打印机 编辑:程序博客网 时间:2024/06/11 13:05


stuff

 

 

The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

 

syntax

STUFF ( character_expression , start , length ,character_expression )

http://msdn.microsoft.com/en-us/library/ms188043.aspx

 

 

NEWID

Creates a unique value of type uniqueidentifier.

 

Syntax

NEWID ( )

return Types

uniqueidentifier

 

 

A. Using the NEWID function with a variable

 

The following example uses NEWID() to assign a value to a variable declared as the uniqueidentifier data type. The value of the uniqueidentifier data type variable is printed before the value is tested.

Copy
-- Creating a local variable with DECLARE/SET syntax.DECLARE @myid uniqueidentifierSET @myid = NEWID()PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)

Here is the result set.

Copy
Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF
 
_______________________________________________________
Q:
使用ADO 的存储过程控件,设置存储过程名字的时候,
TADOStoredProc.ProcedureName="sp_AutoMoney;1" 

其中 ";1" 是什么意思? 
A:
;number是可选的整数,用于将相同名称的过程进行组合,使得它们可以用一句 DROP PROCEDURE 语句除去。该参数不能用于扩展存储过程。在同一应用程序中使用的过程一般都以该方式组合。例如,在订购应用程序中使用的过程可以 orderproc;1、orderproc;2 等来命名。DROP PROCEDURE orderproc 语句将除去整个组。在对过程分组后,不能除去组中的单个过程。例如,DROP PROCEDURE orderproc;2 是不允许的。
Q:
首先寫了個自定義函數 
CREATE FUNCTION f_EmpId()
RETURNS nchar(17)
AS
BEGIN
   
  RETURN(SELECT 'CMI'+CONVERT(varchar,GETDATE(),112)+RIGHT(100+ISNULL(RIGHT(MAX(EmpId),3),0),3) 
  FROM Init_Employee WITH(XLOCK,PAGLOCK))
END
GO

怎樣把它作為表Init_Employee的EmpId默認值啊,我直接在表Init_Employee-修改-選中列EmpId-默認值或綁定-輸入f_EmpId(),出入數據時EmpId的列就是 f_EmpId(),請問應該怎麼寫
A:
CREATE TABLE Init_Employee (char(8) PRIMARY KEY DEFAULT dbo.f_EmpId(),col int)
EmpId
Q:
id sj name
1 19870609 交大
1 19700605 XX中学
2 19990601 一中
3 19980602 XX学院
3 19940601 XX高中
--------------------------------------------
我需要查出
1 19870609 交大
2 19990601 一中
3 19980602 XX学院
一张表里面存的人员的教育经历
id--人员编号 sj--毕业时间 name--学校名称

1 19870609 交大
1 19700605 XX中学
代表1这个人87年交大毕业,70年XX中学毕业
想这样的记录有的是可能就一条,有的人是多条

我需要获取到每个人的最后毕业时的那条记录,也就是最高学历的那条记录
A:
declare @t1 table(id int, sj varchar(8), name varchar(20))
insert into @t1
select 1, '19870609', '交大'
union  select 1, '19700605', 'XX中学'
union  select 2, '19990601', '一中'
union  select 3, '19980602', 'XX学院'
union  select 3, '19940601', 'XX高中'

--方法1
SELECT id,sj,NAME FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY id ORDER BY sj desc) AS rowid,* FROM @t1)
ASWHERE rowid='1'

--方法2
SELECT * FROM @t1 t1 WHERE sj=(SELECT MAX(sj) FROM @t1 t2 WHERE t1.id=t2.id) ORDER BY id

--方法3
SELECT * FROM @t1 t1 WHERE NOT EXISTS(SELECT 1 FROM @t1 WHERE id=t1.id AND sj>t1.sj)
Q:
2.原有数据
ID NAME
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
7 GG
8 HH
进行一次update之后再次select
得到
ID NAME
1 BB
2 BB
3 DD
4 DD
5 FF
6 FF
7 HH
8 HH
这个update怎么写,不能使用case和when
A:
create table tb(ID int,NAME varchar(10))
insert into tb values(1 ,'AA')
insert into tb values(2 ,'BB')
insert into tb values(3 ,'CC')
insert into tb values(4 ,'DD')
insert into tb values(5 ,'EE')
insert into tb values(6 ,'FF')
insert into tb values(7 ,'GG')
insert into tb values(8 ,'HH')
go

update tb set name = (select top 1 name from tb where id > t.id order by id) from tb t where id % 2 = 1

select * from tb
/*
ID          NAME      
----------- ----------
1           BB
2           BB
3           DD
4           DD
5           FF
6           FF
7           HH
8           HH

(所影响的行数为 8 行)
*/

drop table tb
 
Q:
 表table_A:
  路线代码 起点 终点 起点桩号 终点桩号  
  Y440608 大湾 石柱 0.56 2.35  
  Y440608 石柱 观音桥 2.35 8.52
  Y440608 观音桥 芦溪 8.52 11.98
  Y440605 龙桥 磙子河 36.58 56.32

现在要写一条sql 让结果这样
  路线代码 起点 终点 起点桩号 终点桩号  
  Y440608 大湾 芦溪 0.56 11.98
  Y440605 龙桥 磙子河 36.58 56.32

就是按照[ 路线代码]分组后只取一条结果,这个结果是经过处理了的,好人帮忙
A:
CREATE TABLE #temp
(
    路线代码
VARCHAR(100),
    起点
NVARCHAR(100),
    终点
NVARCHAR(100),
    起点桩号
DECIMAL(10,2),
    终点桩号
DECIMAL(10,2)
)
INSERT INTO #temp
SELECT 'Y440608', N'大湾', N'石柱', 0.56, 2.35 UNION ALL
SELECT 'Y440608', N'石柱', N'观音桥', 2.35, 8.52 UNION ALL
SELECT 'Y440608', N'观音桥', N'芦溪', 8.52, 11.98 UNION ALL
SELECT 'Y440605', N'龙桥', N'磙子河', 36.58, 56.32

SELECT a.路线代码, b.起点, c.终点, a.起点桩号, a.终点桩号 FROM
(
SELECT 路线代码, 起点桩号 = MIN(起点桩号), 终点桩号 = MAX(终点桩号) FROM #temp  GROUP BY 路线代码) a
INNER JOIN #temp b
ON a.路线代码 = b.路线代码 AND a.起点桩号 = b.起点桩号
INNER JOIN #temp c
ON a.路线代码 = c.路线代码 AND a.终点桩号 = c.终点桩号
或者用CROSS APPLY连接
SQL code
SELECT a.路线代码, b.起点, c.终点, a.起点桩号, a.终点桩号 FROM(SELECT 路线代码, 起点桩号 = MIN(起点桩号), 终点桩号 = MAX(终点桩号) FROM #temp GROUP BY 路线代码) aCROSS APPLY(SELECT TOP(1) 起点 FROM #temp WHERE 路线代码 = a.路线代码 AND 起点桩号 = a.起点桩号) bCROSS APPLY(SELECT TOP(1) 终点 FROM #temp WHERE 路线代码 = a.路线代码 AND 终点桩号 = a.终点桩号) c
create table table_A(路线代码 varchar(10),起点 varchar(10),终点 varchar(10),起点桩号 decimal(18,2),终点桩号 decimal(18,2))insert into table_a values('Y440608', '大湾' , '石柱' , 0.56 ,2.35)insert into table_a values('Y440608', '石柱' , '观音桥', 2.35 ,8.52)insert into table_a values('Y440608', '观音桥', '芦溪' , 8.52 ,11.98)insert into table_a values('Y440605', '龙桥' , '磙子河', 36.58 ,56.32)goselect 路线代码,max(起点) 起点,max(终点) 终点,max(起点桩号) 起点桩号,max(终点桩号) 终点桩号 from(select t.路线代码, 起点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点 end), 终点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点 end), 起点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点桩号 end), 终点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点桩号 end)from table_a t) mgroup by 路线代码drop table table_a