SQLServer常用代码段

前端之家收集整理的这篇文章主要介绍了SQLServer常用代码段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

------------------------------------------------------------------------
GO
--查询库中表的行数 打印大于0行的表名和行数
DECLARE @TableName VARCHAR(128)
Declare PostCur Cursor For   
SELECT name FROM sys.tables  
Open PostCur  
Fetch next From PostCur Into @TableName  
While @@fetch_status=0  
	Begin  	
	DECLARE @ct INT
	SET @ct=0
	DECLARE @strsql NVARCHAR(500)
	SET @strsql='Select @ct=Count(1) From '[email protected]
	exec sp_executesql @strsql,N'@ct int output',@ct OUTPUT
	IF(@ct>0)
	BEGIN
		print '表名 '[email protected]
		PRINT '数据条数 '+Convert(varchar(32),@ct)
	END
	Fetch next From PostCur Into @TableName    
	End  
Close PostCur  
Deallocate PostCur

----------------------------------------
--查询数据库总各表数据量
GO
CREATE TABLE #tbles(ID int IDENTITY(1,1),TableName varchar(128),IsHandle bit DEFAULT('False'),TableRowCount int DEFAULT(0))
INSERT INTO #tbles(TableName)
SELECT name FROM sys.tables
DECLARE @TableName varchar(128)
DECLARE @ID int
SET @ID=0
SELECT TOP 1 @ID=ID,@TableName=TableName FROM #tbles WHERE IsHandle='False'
WHILE(@ID!=0)
BEGIN
	DECLARE @isexists bit
	DECLARE @RowCount int
	DECLARE @sqlStr nvarchar(1000)
	SET @sqlStr='SELECT @RowCount=Count(1) FROM '[email protected]
	exec sp_executesql @sqlStr,N'@RowCount int output',@RowCount OUTPUT
	UPDATE #tbles SET IsHandle='True',[email protected] WHERE [email protected]
	Print @RowCount
	PRINT @TableName	
	SET @ID=0
	SET @RowCount=0
	SET @sqlStr=''
	SELECT TOP 1 @ID=ID,@TableName=TableName FROM #tbles WHERE IsHandle='False'
END
SELECT * FROM #tbles
Drop Table #tbles


------------------------------------------------------------------------
---查询存储过程返回结果
GO
create proc getdata2
as
select 1 as r1,2 as r2

GO
create table #temp(r1 int,r2 int)
insert into #temp exec getdata2
select * From #temp
drop table #temp


------------------------------------------------------------------------
----查询生成拼接字符串
Go
create table #temp1(c1 int)
insert into #temp1(c1)Values(1)
insert into #temp1(c1)Values(2)
insert into #temp1(c1)Values(3)
insert into #temp1(c1)Values(4)
GO
Select c1 From #temp1 for xml path('')
Select c1 as [data()] From #temp1 for xml path('')
Select Convert(varchar(10),c1) +',' From #temp1 for xml path('')
declare @str varchar(max)
select @str=(Select Convert(varchar(10),' From #temp1 for xml path(''))
select @str


------------------------------------------------------------------------
--获取随机字符串
GO
CREATE VIEW [dbo].[V_RAND]
AS
SELECT RAND1 = CONVERT(INT,RAND()*26),RAND2   =   RAND()*2

GO
Create   FUNCTION   [dbo].[f_GetRandStr](@LEN   INT,@FLAG   INT) 
RETURNS   NVARCHAR(100) 
AS 
[email protected]   输出字符的长度 
[email protected]   返回值包含字符   1:大写字母      2:小写字母      3:大小写字母混合 
BEGIN 
       DECLARE   @sql   NVARCHAR(100),@RAND   INT 
       SELECT   @sql   =    '' 
       IF   @LEN>100 
             SET @LEN   =   100 
  
       WHILE   @LEN>0    
       BEGIN 
             SELECT @RAND   =   RAND1 +(CASE   @FLAG   WHEN   1   THEN   65   WHEN   2   THEN   97 
                   ELSE(CASE WHEN RAND2 > 1 THEN   97   ELSE   65   END)   END) 
             FROM   V_RAND 
  
             SELECT   @[email protected] + CHAR(@RAND),@LEN  = @LEN - 1 
       END 
  
       RETURN   @sql 
END
GO
Select dbo.f_GetRandStr(30,3)

----------------------------------------
--简单的传参输出
GO
declare @i3 int
exec sp_executesql N'Select @[email protected][email protected]',N'@i1 int,@i2 int,@i3 int output',1,22,@i3 output
Select @i3

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的MySQL相关文章