RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
多层开发中,经常写SQL SERVER存储过程的朋友看过
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 10:37
  • 来源:未知

 

下面是自动产生存储过程的sql 脚本,你可以在查询分析器中运行.

运行完,你会看到多了四个存储过程

pr__SYS_MakeInsertRecordProc

pr__SYS_MakeUpdateRecordProc

pr__SYS_MakeSelectRecordProc

pr__SYS_MakeDeleteRecordProc

执行方式:在查询分析器中执行

pr__SYS_MakeInsertRecordProc '表名'         --得到插入语句

pr__SYS_MakeInsertRecordProc '表名',1    --得到插入语句,并创建Insert存储过程

 

完整脚本如下:

SET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO

 

 

CREATE      PROC pr__SYS_MakeDeleteRecordProc @sTableName varchar(128), @bExecute bit = 0AS

IF dbo.fnTableHasPrimaryKey(@sTableName) = 0 BEGIN RAISERROR ('Procedure cannot be created on a table with no primary key.', 10, 1) RETURN END

DECLARE @sProcText varchar(8000), @sKeyFields varchar(2000), @sWhereClause varchar(2000), @sColumnName varchar(128), @nColumnID smallint, @bPrimaryKeyColumn bit, @nAlternateType int, @nColumnLength int, @nColumnPrecision int, @nColumnScale int, @IsNullable bit,  @IsIdentity int, @sTypeName varchar(128), @sDefaultValue varchar(4000), @sCRLF char(2), @sTAB char(1)

SET @sTAB = char(9)SET  @sCRLF = char(13) + char(10)

SET  @sProcText = ''SET  @sKeyFields = ''SET @sWhereClause = ''

SET  @sProcText = @sProcText + 'IF EXISTS(SELECT * FROM sysobjects WHERE name = ''prApp_' + @sTableName + '_Delete'')' + @sCRLFSET  @sProcText = @sProcText + @sTAB + 'DROP PROC prApp_' + @sTableName + '_Delete' + @sCRLFIF @bExecute = 0 SET  @sProcText = @sProcText + 'GO' + @sCRLF

SET  @sProcText = @sProcText + @sCRLF

PRINT @sProcText

IF @bExecute = 1  EXEC (@sProcText)

SET  @sProcText = ''SET  @sProcText = @sProcText + '----------------------------------------------------------------------------' + @sCRLFSET  @sProcText = @sProcText + '-- Delete a single record from ' + @sTableName + @sCRLFSET  @sProcText = @sProcText + '----------------------------------------------------------------------------' + @sCRLFSET  @sProcText = @sProcText + 'CREATE PROC prApp_' + @sTableName + '_Delete' + @sCRLF

DECLARE crKeyFields cursor for SELECT * FROM dbo.fnTableColumnInfo(@sTableName) ORDER BY 2

OPEN crKeyFields

FETCH  NEXT FROM  crKeyFields INTO  @sColumnName, @nColumnID, @bPrimaryKeyColumn, @nAlternateType,  @nColumnLength, @nColumnPrecision, @nColumnScale, @IsNullable,  @IsIdentity, @sTypeName, @sDefaultValue    WHILE (@@FETCH_STATUS = 0) BEGIN