找出数据库中所有的数据库名:
DECLARE @databaseName VARCHAR(100)DECLARE @itemCur CURSORSET @itemCur = CURSOR FOR SELECT name FROM sys.databases WHERE database_id > 4OPEN @itemCurFETCH NEXT FROM @itemCur INTO @databaseName--读取下一条游标是否成功WHILE @@FETCH_STATUS=0BEGIN --逻辑处理 PRINT @databaseName FETCH NEXT FROM @itemCur INTO @databaseNameEND CLOSE @itemCurDEALLOCATE @itemCur
鼠标轨迹字字符串分割:
1.创建测试表
--创建测试表[VisiteLog]CREATE TABLE [dbo].[VisiteLog]( [Id] [int] IDENTITY(1,1) NOT NULL, [PosSet] [varchar](500) NULL, CONSTRAINT [PK_VisiteLog] PRIMARY KEY CLUSTERED ( [Id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO--插入测试数据SET IDENTITY_INSERT [dbo].[VisiteLog] ONINSERT [dbo].[VisiteLog] ([Id], [PosSet]) VALUES (1, N'[573,1103,2010-09-03 22:32:35],[864,110,2010-09-03 22:32:57]')INSERT [dbo].[VisiteLog] ([Id], [PosSet]) VALUES (2, N'[513,1303,2010-09-03 22:34:35]')SET IDENTITY_INSERT [dbo].[VisiteLog] OFF--查看数据SELECT * FROM [dbo].[VisiteLog]
结果:
2.创建表PosSetInfo,这个表是用来保存鼠标轨迹字符串分割后的数据
--创建表[PosSetInfo]CREATE TABLE [dbo].[PosSetInfo]( [ID] [int] NULL, [PosSet_x] [int] NULL, [PosSet_y] [int] NULL, [PosTime] [datetime] NULL) ON [PRIMARY]GO
3.创建了一个函数,它的作用是把字符串@str以@split分隔符进行分隔,返回第@index次匹配的元素。
CREATE FUNCTION [dbo].[Get_StrArrayStrOfIndex]( @str VARCHAR(5000), --要分割的字符串 @split VARCHAR(10), --分隔符号 @index INT --取第几个元素)RETURNS VARCHAR(5000)ASBEGIN DECLARE @location INT DECLARE @start INT DECLARE @next INT DECLARE @seed INT SET @str=LTRIM(RTRIM(@str)) SET @start=1 SET @next=1 SET @seed=LEN(@split) SET @location=CHARINDEX(@split,@str) WHILE @location<>0 and @index>@next BEGIN SET @start=@location+@seed SET @location=CHARINDEX(@split,@str,@start) SET @next=@next+1 END IF @location =0 SELECT @location =LEN(@str)+1 RETURN SUBSTRING(@str,@start,@location-@start)END--测试函数SELECT DBO.[Get_StrArrayStrOfIndex]('AB,CDE,FG,HIJK',',',3);
4.VisiteLog表的数据进行分割,再把分割后的字符串保存到PosSetInfo表中
--鼠标轨迹字符串分割DECLARE @ID int,@PosSet VARCHAR(2000)DECLARE @splitlen INTDECLARE @split NVARCHAR(10)DECLARE @temp VARCHAR(100)DECLARE @PosSet_x INTDECLARE @PosSet_y INTDECLARE @PosTime DATETIMESET @split = ']'DECLARE @itemCur CURSORSET @itemCur = CURSOR FOR SELECT ID,PosSet FROM [VisiteLog] WHERE PosSet <> ''OPEN @itemCurFETCH NEXT FROM @itemCur INTO @ID,@PosSetWHILE @@FETCH_STATUS=0BEGIN SET @splitlen=LEN(@split+'a')-2 WHILE CHARINDEX(@split,@PosSet)>0 BEGIN SET @Temp = LEFT(@PosSet,CHARINDEX(@split,@PosSet)-1) SET @Temp = replace(replace(@Temp,',[',''),'[','') SET @PosSet_x = dbo.Get_StrArrayStrOfIndex(@Temp,',',1) SET @PosSet_y = dbo.Get_StrArrayStrOfIndex(@Temp,',',2) SET @PosTime = dbo.Get_StrArrayStrOfIndex(@Temp,',',3) INSERT PosSetInfo VALUES(@ID,@PosSet_x,@PosSet_y,@PosTime) SET @PosSet=STUFF(@PosSet,1,CHARINDEX(@split,@PosSet)+@splitlen,'') END FETCH NEXT FROM @itemCur INTO @ID,@PosSetEND CLOSE @itemCurDEALLOCATE @itemCur
5.看PosSetInfo表的数据
SELECT * FROM [dbo].[PosSetInfo]
6.补充:
--循环每个数据库EXEC sp_MSForEachDB 'USE [?];SELECT * FROM sys.database_files'