日期:2014-05-18 浏览次数:20517 次
--编写 create proc proc_name as select 1 go --调用 exec proc_name --这个算简单的不
------解决方案--------------------
--创建 create proc proc_abc as print 'abc' --调用 exec proc_abc
------解决方案--------------------
我支持初学者,但是你自己的看懂
CREATE PROCEDURE proc_Page @Table VARCHAR(100), --表名 @Primarykey VARCHAR(100), --主键 @Condition VARCHAR(500), --查询条件 @PageNumber INT, --开始页数 @PageSize INT, --每页大小 @IsCount BIT --是否获得记录数,0为否 AS DECLARE @SQL VARCHAR(8000) IF @IsCount != 0 SET @SQL = 'SELECT Count(*) FROM ' + @Table + ' WHERE ' + @Condition ELSE BEGIN IF @PageNumber = 1 SET @SQL = 'SELECT TOP ' + STR(@PageSize) + ' SerialNum AS 流水号,PatientName AS 病人姓名,CheckDate AS 检查日期 FROM ' + @Table + ' WHERE ' + @Condition ELSE SET @SQL = 'SELECT TOP ' + STR(@PageSize) + ' SerialNum AS 流水号,PatientName AS 病人姓名,CheckDate AS 检查日期 FROM ' + @Table + ' WHERE ' + @Primarykey + ' NOT IN (SELECT TOP ' + STR(@PageSize*(@PageNumber - 1)) + ' ' + @Primarykey + ' FROM ' + @Table + ' WHERE ' + @Condition + ') AND ' + @Condition END EXEC(@SQL) RETURN
------解决方案--------------------
来个复杂的
create procedure TestProc @Id int, --参数1 @Name varchar(50) --参数2 as select @id,@Name go --调用 exec TestProc @Id=18,@Name='张三'
------解决方案--------------------
CREATE PROCEDURE [dbo].[P_GetTableRecord2] @TableName varchar(50) AS BEGIN SET NOCOUNT ON DECLARE @SQL varchar(500) SET @SQL='IF OBJECT_ID('''+@TableName+''') IS NOT NULL ' SET @SQL=@SQL+CHAR(10)+' SELECT COUNT(*) AS FRowCount FROM '+@TableName SET @SQL=@SQL+CHAR(10)+'ELSE' SET @SQL=@SQL+CHAR(10)+' SELECT 0 AS FRowCount' PRINT(@SQL) exec(@SQL) SET NOCOUNT OFF END
------解决方案--------------------
--得到服务器的IP地址 create proc [dbo].[getip] as create table #ip(id int identity(1,1),re varchar(200)) declare @s varchar(1000) select @s= 'ping '+host_name()+' -a -n 1 -l 1' --set @s='ping '+left(@@servername,charindex('\',@@servername+'\')-1)+' -a -n 1 -l 1' insert #ip(re) exec master..xp_cmdshell @s select IP地址=stuff(left(re,charindex(']',re)-1),1,charindex('[',re),'') from #ip where id=2 drop table #ip