set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[csp_MakeUpdate]
(
@TABLENAME VARCHAR(255)= 'xyzzy',
@LANG CHAR(2) = '' -- VB or C# (default set in configration section)
) AS
-- Purpose:
-- Create update script from table definition
-- Copyright (C) 2000, 2003, 2004, 2005 Clive Chinery
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the
-- Free Software Foundation, Inc., 59 Temple Place, Suite 330,
-- Boston, MA 02111-1307 USA
-- Notes:
-- 1) Run from the database under development
-- csp_MakeUpdate 'TABLENAME', 'XX'
-- where XX is C# or VB
-- 2) The TABLENAME is case insensitive, however the table must exist
-- 3) Id and TIMESTAMP columns are specially handled
-- 4) Tables with Id column will have a where clause using the
-- Id column - this normally suffices.
-- Changes:
-- 11Nov2007 Clive Convert to SQL2005 Operation (n.b. this cannot reside in Master as a result)
-- 24Jun2006 Clive Make VB/C# switchable
-- 09Oct2005 Clive Update documentation
-- 01Sep2005 Clive Extend to cover more data types
-- 09Jun2005 Clive Make c# variant of vb.net
-- 14Feb2005 Clive Autogenerate data access code
-- 14Oct2000 Clive Tidied output
-- 01Aug2000 Clive Documentation update
-- 31Jul2000 Clive Modified to put more of documentation header
SET NOCOUNT ON
DECLARE @ColName VARCHAR(80)
DECLARE @COL_NAME VARCHAR(80)
DECLARE @ColLength INT
DECLARE @HasIdent INT
DECLARE @HasTSTAMP INT
DECLARE @RowCount INT
DECLARE @RowIndex INT
DECLARE @PrintLine VARCHAR(256)
DECLARE @SpName VARCHAR(80)
DECLARE @DateWork VARCHAR(16)
DECLARE @TN VARCHAR(80)
DECLARE @USER VARCHAR(80)
DECLARE @colid INT
DECLARE @xprec INT
DECLARE @xscale INT
DECLARE @DOM_NAME VARCHAR(50)
DECLARE @SpPrefix VARCHAR(10)
DECLARE @SET_DB VARCHAR(50)
DECLARE @ObPrefix VARCHAR(10)
DECLARE @DefLanguage CHAR(2)
DECLARE @Separator2 VARCHAR(1)
DECLARE @SqlDbType VARCHAR(20)
DECLARE @COMMENT VARCHAR(128)
DECLARE @ALLOWED BIT
SELECT @ALLOWED = 0
SELECT @LANG = RTRIM(UPPER(LTRIM(@LANG)))
IF @LANG = 'VB' SELECT @ALLOWED = 1
IF @LANG = 'C#' SELECT @ALLOWED = 1
-- Configuration start
SELECT @SpPrefix = 'usp_' -- Set to desired prefix for stored procedures
SELECT @DOM_NAME = 'DALETH' + '\' -- Set to your domain name
-- Set @SET_DB to name of SP to set permissions or to blank
SELECT @SET_DB = 'asp_dbPermissions'
SELECT @ObPrefix = 'x' -- Prefix for object variables
SELECT @DefLanguage = 'VB' -- Default language C# or VB
SELECT @Separator2 = '' -- Separator between Tablename and action
-- Configuration end
IF @ALLOWED = 0 SET @LANG = @DefLanguage
IF @TABLENAME ='xyzzy'
OR (NOT EXISTS (SELECT * FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'))
BEGIN
PRINT '--Use csp_MakeUpdate TABLENAME (XX) to generate update '
PRINT 'S.P. for TABLENAME and XX is language for Wrapper VB or C#'
PRINT '--or csp_MakeUpdate garbage to get this help text!'
PRINT 'GO'
RETURN
END
SELECT @TN = name FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'
DECLARE @DocTable TABLE
(
Comment VARCHAR(128),
Colname VARCHAR(128),
Coltype VARCHAR(128),
Cstatus INT,
ColId INT,
Cid INT,
length INT,
xprec INT,
xscale INT
)
INSERT INTO @DocTable (Comment, Colname, Coltype, Cstatus,
ColId, Cid, length, xprec, xscale)
SELECT COALESCE(CONVERT(VARCHAR(130),P.value),
'[Undocumented in Database]') AS Comment,
C.name AS Colname, UPPER(U.name) AS Coltype, C.status AS Cstatus,
C.ColId, C.Id AS Cid, C.length, C.xprec, C.xscale
FROM syscolumns C
JOIN sys.tables O ON C.id = O.object_id
JOIN systypes U ON C.xusertype = U.xusertype
LEFT OUTER JOIN SYS.extended_properties P ON P.minor_id = C.COLID AND C.id = P.major_id
WHERE O.name = @TN AND U.name <> 'timestamp'
ORDER BY C.colid
SELECT @HasTSTAMP = (SELECT COUNT(*) FROM @DocTable WHERE Colname =
-- Begin updateable
'UpdateCount')
-- End Updateable
SELECT @SpName = @SpPrefix + RTRIM(@TN) + @Separator2 + 'Update'
SELECT @ColLength = MAX(DATALENGTH(RTRIM(CONVERT(VARCHAR(80),Colname))))
FROM @DocTable
WHERE Cstatus <> 128
IF @ColLength < 14 SET @ColLength = 14
IF EXISTS(select * from sysobjects
WHERE UPPER(name) = UPPER(@SpName))
SELECT @PrintLine='ALTER PROCEDURE dbo.' + @SpName
ELSE
SELECT @PrintLine='CREATE PROCEDURE dbo.' + @SpName
----
PRINT @PrintLine
PRINT '('
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Coltype <> 'timestamp' AND Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate','UpdateDate')
-- End Updateable
SELECT @HasIdent=COUNT(*) FROM @DocTable WHERE Cstatus=128
DECLARE xArgs CURSOR FOR
SELECT '@' + Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Coltype <> 'timestamp' AND Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate', 'UpdateDate')
-- End Updateable
ORDER BY colid
DECLARE @ArgName VARCHAR(80),
@Type VARCHAR(32),
@Length INT
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @ArgName, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
SELECT @ArgName = SUBSTRING(@ArgName
+ ' ',1,@ColLength + 4)
SELECT @PrintLine = ' ' + @ArgName + @Type
IF @Type='char'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length)) + ')'
IF @Type='varchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length)) + ')'
IF @Type='nchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length/2)) + ')'
IF @Type='nvarchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length/2)) + ')'
IF (@Type='decimal') OR (@Type='numeric')
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@xprec))
+ ',' + RTRIM(convert(varchar(10),@xscale)) + ')'
SELECT @PrintLine = @PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
PRINT ' @Return INT OUTPUT'
PRINT ') AS'
PRINT '-- Purpose:'
SELECT @PrintLine='-- Update record on ' + @TN + ' table'
PRINT @PrintLine
PRINT '-- Parameters:'
DECLARE xArgs CURSOR FOR
SELECT Colname, Comment FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate','UpdateDate')
-- End Updateable
ORDER BY colid
OPEN xArgs
FETCH xArgs INTO @ArgName, @COMMENT
WHILE @@FETCH_status=0
BEGIN
SELECT @ArgName=SUBSTRING(@ArgName
+ ' ',1,@ColLength + 2)
SELECT @PrintLine='-- ' + @ArgName + '- ' + @COMMENT
PRINT @PrintLine
FETCH xArgs INTO @ArgName, @COMMENT
END
CLOSE xArgs
DEALLOCATE xArgs
PRINT '-- Return - Return value'
PRINT '-- History: '
SELECT @USER = system_user
IF SUBSTRING(@USER,1,DATALENGTH(@DOM_NAME)) = @DOM_NAME BEGIN
SELECT @USER = RTRIM(SUBSTRING(@USER,DATALENGTH(@DOM_NAME) + 1,20))
END
SELECT @DateWork = CONVERT(CHAR(11),CURRENT_TIMESTAMP,106)
SELECT @DateWork = substring(@DateWork,1,2)
+ SUBSTRING(@DateWork,4,3) + SUBSTRING(@DateWork,8,4)
SELECT @PrintLine = '-- ' + @DateWork + ' '
SELECT @PrintLine = @PrintLine + @USER + ' Original coding'
PRINT @PrintLine
PRINT 'SET NOCOUNT ON'
IF @HasIdent > 0 AND (@HasTSTAMP > 0) BEGIN
SELECT @PrintLine='IF EXISTS(SELECT * FROM ' + @TN + '
WHERE Id = @Id AND UpdateCount = @UpdateCount) BEGIN '
PRINT @PrintLine
END
SELECT @PrintLine=' UPDATE ' + @TN + ' SET'
PRINT @PrintLine
DECLARE @SetValue VARCHAR(80)
SELECT @RowCount = COUNT(*) FROM @DocTable
WHERE Colname NOT IN ('CreatePersonId','CreateDate')
AND Cstatus <> 128
DECLARE xArgs CURSOR FOR
SELECT Colname FROM @DocTable
WHERE Colname NOT IN ('CreatePersonId','CreateDate')
AND Cstatus <> 128
ORDER BY colid
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @ArgName
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex=@RowIndex + 1
SELECT @ArgName=SUBSTRING(@ArgName
+ ' ',1,@ColLength+2)
IF @ArgName = 'UpdateDate ' BEGIN
SELECT @PrintLine = ' ' + @ArgName + '= GETUTCDATE()'
END
ELSE BEGIN
IF @ArgName = 'UpdateCount' BEGIN
SELECT @PrintLine = ' ' + @ArgName
+ ' = UpdateCount + 1'
END
ELSE BEGIN
SELECT @PrintLine = ' ' + @ArgName
+ ' = @' + RTRIM(@ArgName)
END
END
IF @RowIndex <> @RowCount
SELECT @PrintLine=@PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName
END
CLOSE xArgs
DEALLOCATE xArgs
SELECT @RowCount = COUNT(*) FROM @DocTable
-- There is a missing where clause here!
IF @RowCount = 0 BEGIN
PRINT '-- You MUST amend the following line to '
+ 'correctly identify the row to update'
PRINT 'WHERE SOMECOLUMN = @SOMEVALUE'
PRINT ''
PRINT 'SELECT @Return = @@error'
PRINT 'RETURN'
END
ELSE BEGIN
IF (@HasIdent > 0) AND (@HasTSTAMP > 0) BEGIN
SELECT @PrintLine
=' WHERE Id = @Id AND UpdateCount = @UpdateCount '
PRINT @PrintLine
PRINT ' SELECT @Return = @@error'
PRINT ' RETURN'
PRINT ' END '
PRINT 'ELSE '
PRINT ' SELECT @Return = 13'
PRINT ' RETURN'
END
ELSE BEGIN
PRINT 'WHERE '
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Cstatus=128
DECLARE xArgs CURSOR FOR
SELECT Colname + ' = @' + Colname
FROM @DocTable
WHERE Cstatus=128
ORDER BY Colid
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @SetValue
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
SELECT @PrintLine = ' ' + @SetValue
IF @RowIndex <> @RowCount
SELECT @PrintLine = @PrintLine + ' AND'
PRINT @PrintLine
FETCH xArgs INTO @SetValue
END
CLOSE xArgs
DEALLOCATE xArgs
IF @HasTSTAMP = 1 BEGIN
PRINT ' AND UpdateCount = @UpdateCount'
------
PRINT ''
END
PRINT 'SELECT @Return = @@error'
PRINT 'RETURN'
END
END
PRINT '-------------- this is the end ----------------'
PRINT '/* Data Access code follows:'
SELECT @RowIndex = 0
IF @LANG = 'VB' BEGIN
PRINT ' #Region " Update "'
PRINT ' '''''' <summary> '
PRINT ' '''''' Update record on ' + @TN + ' table '
PRINT ' '''''' </summary>'
PRINT ' '''''' <remarks>'
PRINT ' '''''' This code was autogenerated on ' + @DateWork
PRINT ' '''''' </remarks>'
END
ELSE BEGIN
PRINT ' #region " Update "'
PRINT ' /// <summary> '
PRINT ' /// Update record on ' + @TN + ' table '
PRINT ' /// </summary>'
PRINT ' /// <remarks>'
PRINT ' /// This code was autogenerated on ' + @DateWork
PRINT ' /// </remarks>'
END
DECLARE xArgs CURSOR FOR -- need comment fetch
SELECT Comment, Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId
OPEN xArgs
FETCH xArgs INTO @COMMENT, @COL_NAME, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
IF @LANG = 'VB'
SELECT @PrintLine = ' '''''' <param name="'
ELSE
SELECT @PrintLine = ' /// <param name="'
SELECT @SqlDbType = 'x' -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'l'
IF @Type='bit' SELECT @SqlDbType = 'b'
IF @Type='int' SELECT @SqlDbType = 'i'
IF @Type='decimal' SELECT @SqlDbType = 'c'
IF @Type='smallmoney' SELECT @SqlDbType = 'c'
IF @Type='numeric' SELECT @SqlDbType = 'c'
IF @Type='datetime' SELECT @SqlDbType = 'dat'
IF @Type='smalldatetime' SELECT @SqlDbType = 'dat'
IF @Type='char' SELECT @SqlDbType = 's'
IF @Type='varchar' SELECT @SqlDbType = 's'
IF @Type='nchar' SELECT @SqlDbType = 's'
IF @Type='nvarchar' SELECT @SqlDbType = 's'
IF @Type='text' SELECT @SqlDbType = 's'
IF @Type='ntext' SELECT @SqlDbType = 's'
--IF @COL_NAME = 'UpdatePersonId'
-- SELECT @COMMENT = 'Id of user from GetUserPersonId'
--IF @COL_NAME = 'UpdateCount'
-- SELECT @COMMENT = 'Count of existing updates to row'
-- More comment signs here ?
SELECT @PrintLine = @PrintLine + @SqlDbType + @COL_NAME
+ '">' + @COMMENT + '</param>'
PRINT @PrintLine
FETCH xArgs INTO @COMMENT, @COL_NAME, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' '''''' <param name="iReturn">Return code to '
+ 'use in switch statement</param>'
PRINT ' Public Sub Update' + @TN + '(iId As Integer, _'
END
ELSE BEGIN
PRINT ' /// <param name="iReturn">Return code to use'
+ ' in switch statement</param>'
PRINT ' public void Update' + @TN + '(int iId,'
END
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
DECLARE @PREFIX VARCHAR(10)
DECLARE xArgs CURSOR FOR
SELECT Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId
OPEN xArgs
FETCH xArgs INTO @COL_NAME, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
IF @RowIndex > 1 BEGIN
SELECT @SqlDbType = @Type -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'Int64'
IF @Type='smallint' SELECT @SqlDbType = 'Int16'
IF @Type='bit' SELECT @SqlDbType = 'bool'
IF @Type='int' SELECT @SqlDbType = 'int'
IF @Type='decimal' SELECT @SqlDbType = 'decimal'
IF @Type='numeric' SELECT @SqlDbType = 'decimal'
IF @Type='datetime' SELECT @SqlDbType = 'DateTime'
IF @Type='smalldatetime' SELECT @SqlDbType = 'DateTime'
IF @Type='char' SELECT @SqlDbType = 'string'
IF @Type='varchar' SELECT @SqlDbType = 'string'
IF @Type='nchar' SELECT @SqlDbType = 'string'
IF @Type='nvarchar' SELECT @SqlDbType = 'string'
IF @Type='text' SELECT @SqlDbType = 'string'
IF @Type='ntext' SELECT @SqlDbType = 'string'
IF @Type='smallmoney' SELECT @SqlDbType = 'decimal'
IF @Type='money' SELECT @SqlDbType = 'decimal'
IF @Type='real' SELECT @SqlDbType = 'double'
IF @Type='float' SELECT @SqlDbType = 'double'
SELECT @PREFIX = 'x' -- Default assignment
IF @Type='bigint' SELECT @PREFIX = 'l'
IF @Type='bit' SELECT @PREFIX = 'b'
IF @Type='int' SELECT @PREFIX = 'i'
IF @Type='decimal' SELECT @PREFIX = 'c'
IF @Type='numeric' SELECT @PREFIX = 'c'
IF @Type='datetime' SELECT @PREFIX = 'dat'
IF @Type='smalldatetime' SELECT @PREFIX = 'dat'
IF @Type='char' SELECT @PREFIX = 's'
IF @Type='varchar' SELECT @PREFIX = 's'
IF @Type='nchar' SELECT @PREFIX = 's'
IF @Type='nvarchar' SELECT @PREFIX = 's'
IF @Type='text' SELECT @PREFIX = 's'
IF @Type='ntext' SELECT @PREFIX = 's'
IF @Type='smallmoney' SELECT @PREFIX = 'c'
IF @Type='money' SELECT @PREFIX = 'c'
IF @Type='real' SELECT @PREFIX = 'd'
IF @Type='float' SELECT @PREFIX = 'd'
SELECT @PrintLine = ' '
IF @LANG = 'VB' BEGIN
IF @SqlDbType = 'int' SELECT @SqlDbType = 'Integer'
IF @SqlDbType = 'string' SELECT @SqlDbType = 'String'
IF @SqlDbType = 'decimal' SELECT @SqlDbType = 'Decimal'
IF @SqlDbType = 'bit' SELECT @SqlDbType = 'Boolean'
IF @SqlDbType = 'bool' SELECT @SqlDbType = 'Boolean'
SELECT @PrintLine = @PrintLine
+ @PREFIX + @COL_NAME + ' As '
SELECT @PrintLine = @PrintLine + @SqlDbType -- + ' '
END
ELSE BEGIN
SELECT @PrintLine = @PrintLine + @SqlDbType + ' '
SELECT @PrintLine = @PrintLine + @PREFIX + @COL_NAME
END
IF @RowIndex = @RowCount
IF @LANG = 'VB'
SELECT @PrintLine = @PrintLine
+ ', ByRef iReturn As DatabaseReturnCode)'
ELSE
SELECT @PrintLine = @PrintLine
+ ', ref DatabaseReturnCode iReturn)'
ELSE BEGIN
SELECT @PrintLine = @PrintLine + ', '
IF @LANG = 'VB' SELECT @PrintLine = @PrintLine + ' _ '
END
PRINT @PrintLine
END
FETCH xArgs INTO @COL_NAME, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' iReturn = DatabaseReturnCode.NoErrorDetected'
PRINT ' Dim sConnect As String = CommonData.GetConnect()'
PRINT ' Dim ' + @ObPrefix
+ 'SqlConnection As SqlConnection = new SqlConnection(sConnect)'
PRINT ' Dim ' + @ObPrefix
+ 'SqlCommand As SqlCommand = New SqlCommand("'
+ @SpName + '", ' + @ObPrefix + 'SqlConnection)'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.CommandType = CommandType.StoredProcedure'
PRINT ' Try'
END
ELSE BEGIN
PRINT ' {'
PRINT ' iReturn = DatabaseReturnCode.NoErrorDetected;'
PRINT ' string sConnect = CommonData.GetConnect();'
PRINT ' SqlConnection ' + @ObPrefix
+ 'SqlConnection = new SqlConnection(sConnect);'
PRINT ' SqlCommand ' + @ObPrefix
+ 'SqlCommand = new SqlCommand("'
+ @SpName + '", ' + @ObPrefix + 'SqlConnection);'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.CommandType = CommandType.StoredProcedure;'
PRINT ' try'
PRINT ' {'
END
SELECT @RowIndex = 0
DECLARE xArgs CURSOR FOR
SELECT DISTINCT Colname, Coltype, length, ColId, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('REPLICASTAMP','CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId, Colname
OPEN xArgs
FETCH xArgs INTO @COL_NAME, @Type, @Length, @colid, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@'
+ @COL_NAME + '", SqlDbType.'
SELECT @SqlDbType = @Type -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'BigInt'
IF @Type='bit' SELECT @SqlDbType = 'Bit'
IF @Type='int' SELECT @SqlDbType = 'Int'
IF @Type='decimal' SELECT @SqlDbType = 'Decimal'
IF @Type='numeric' SELECT @SqlDbType = 'Decimal'
IF @Type='datetime' SELECT @SqlDbType = 'DateTime'
IF @Type='char' SELECT @SqlDbType = 'Char'
IF @Type='varchar' SELECT @SqlDbType = 'VarChar'
IF @Type='nchar' SELECT @SqlDbType = 'NChar'
IF @Type='nvarchar' SELECT @SqlDbType = 'NVarChar'
IF @Type='ntext' SELECT @SqlDbType = 'NText'
IF @Type='text' SELECT @SqlDbType = 'Text'
IF @Type='real' SELECT @SqlDbType = 'Real'
IF @Type='smalldatetime' SELECT @SqlDbType = 'SmallDateTime'
IF @Type='smallint' SELECT @SqlDbType = 'SmallInt'
IF @Type='smallmoney' SELECT @SqlDbType = 'SmallMoney'
-------------
IF @Type='float' SELECT @SqlDbType = 'Float'
-- more type conversions here !
SELECT @PrintLine = @PrintLine + @SqlDbType
IF @Type='char' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='varchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='nchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @Type='nvarchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @Type='text' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='ntext' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @LANG = 'C#'
SELECT @PrintLine=@PrintLine + ');'
ELSE
SELECT @PrintLine=@PrintLine + ')'
PRINT @PrintLine
IF (@Type='decimal') OR (@Type='numeric') BEGIN
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Precision = '
+ CONVERT(VARCHAR(3), @xprec)
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Precision = '
+ CONVERT(VARCHAR(3), @xprec) + ';'
PRINT @PrintLine
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Scale = '
+ CONVERT(VARCHAR(3), @xscale)
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Scale = '
+ CONVERT(VARCHAR(3), @xscale) + ';'
PRINT @PrintLine
END
SELECT @RowIndex=@RowIndex + 1
IF @RowIndex = 1
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Value=iId'
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Value=iId;'
ELSE BEGIN
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Value = '
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Value = '
SELECT @SqlDbType = 'x' -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'l'
IF @Type='bit' SELECT @SqlDbType = 'b'
IF @Type='int' SELECT @SqlDbType = 'i'
IF @Type='decimal' SELECT @SqlDbType = 'c'
IF @Type='numeric' SELECT @SqlDbType = 'c'
IF @Type='datetime' SELECT @SqlDbType = 'dat'
IF @Type='smalldatetime' SELECT @SqlDbType = 'dat'
IF @Type='char' SELECT @SqlDbType = 's'
IF @Type='varchar' SELECT @SqlDbType = 's'
IF @Type='nchar' SELECT @SqlDbType = 's'
IF @Type='nvarchar' SELECT @SqlDbType = 's'
IF @Type='text' SELECT @SqlDbType = 's'
IF @Type='ntext' SELECT @SqlDbType = 's'
IF @Type='smallmoney' SELECT @SqlDbType = 'c'
IF @Type='money' SELECT @SqlDbType = 'c'
IF @SqlDbType = 'c' BEGIN
SELECT @PrintLine = @PrintLine + '' + @ObPrefix
+ 'CommonData.DecimalAdjust('
END
SELECT @PrintLine = @PrintLine + @SqlDbType + @COL_NAME
IF @SqlDbType = 'c' BEGIN
SELECT @PrintLine = @PrintLine + ', '
+ RTRIM(convert(varchar(10),@xscale)) + ')'
END
END
IF @LANG = 'VB'
PRINT @PrintLine
ELSE
PRINT @PrintLine + ';'
FETCH xArgs INTO @COL_NAME, @Type, @Length, @colid,
@xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@Return", SqlDbType.Int)'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters("@Return").Direction '
+ '= ParameterDirection.Output'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Open()'
PRINT ' ' + @ObPrefix + 'SqlCommand.ExecuteNonQuery()'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close()'
-- iReturn = Ctype(CommonData.NullToInteger(xSqlCommand.Parameters("@Return").Value),DatabaseReturnCode)
PRINT ' iReturn = Ctype(CommonData.NullToInteger(' + @ObPrefix
+ 'SqlCommand.Parameters("@Return").Value), DatabaseReturnCode)'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose()'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose()'
END
ELSE BEGIN
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@Return", SqlDbType.Int);'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters["@Return"].Direction = ParameterDirection.Output;'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Open();'
PRINT ' ' + @ObPrefix + 'SqlCommand.ExecuteNonQuery();'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close();'
PRINT ' iReturn = (DatabaseReturnCode)CommonData.NullToInteger(' + @ObPrefix
+ 'SqlCommand.Parameters["@Return"].Value);'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose();'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose();'
PRINT ' }'
END
IF @LANG = 'VB' BEGIN
PRINT ' Catch ex As System.InvalidOperationException'
PRINT ' Dim sMethod As String = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString()'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "")'
PRINT ' iReturn = DatabaseReturnCode.NetworkError'
PRINT ' Catch ex As Exception'
PRINT ' Dim sMethod As String = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString()'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "iId="'
+ ' & iId.ToString())'
PRINT ' iReturn = DatabaseReturnCode.OtherError'
PRINT ' Finally '
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close()'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose()'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose()'
PRINT ' End Try '
PRINT ' End Sub'
PRINT ' #End Region'
END
ELSE BEGIN
PRINT ' catch (System.InvalidOperationException ex)'
PRINT ' {'
PRINT ' string sMethod = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name;'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "");'
PRINT ' iReturn = DatabaseReturnCode.NetworkError;'
PRINT ' }'
PRINT ' catch (Exception ex)'
PRINT ' {'
PRINT ' string sMethod = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name;'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "");'
PRINT ' iReturn = DatabaseReturnCode.OtherError;'
PRINT ' }'
PRINT ' finally '
PRINT ' {'
PRINT ' if (' + @ObPrefix + 'SqlCommand.Connection != null) '
+ @ObPrefix + 'SqlCommand.Connection.Close();'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose();'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose();'
PRINT ' }'
PRINT ' }'
PRINT ' #endregion'
END
PRINT '*/'
PRINT 'GO'
PRINT @SET_DB
PRINT 'GO'
RETURN
-------------- this is the end ----------------
GO
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
TATWORTH
All-Star
72405 Points
14018 Posts
MVP
Re: filling the textbox,dropdownlist from the database??
Mar 08, 2008 11:44 AM|LINK
The stored procedure that generated it was:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[csp_MakeUpdate]
(
@TABLENAME VARCHAR(255)= 'xyzzy',
@LANG CHAR(2) = '' -- VB or C# (default set in configration section)
) AS
-- Purpose:
-- Create update script from table definition
-- Copyright (C) 2000, 2003, 2004, 2005 Clive Chinery
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the
-- Free Software Foundation, Inc., 59 Temple Place, Suite 330,
-- Boston, MA 02111-1307 USA
-- Notes:
-- 1) Run from the database under development
-- csp_MakeUpdate 'TABLENAME', 'XX'
-- where XX is C# or VB
-- 2) The TABLENAME is case insensitive, however the table must exist
-- 3) Id and TIMESTAMP columns are specially handled
-- 4) Tables with Id column will have a where clause using the
-- Id column - this normally suffices.
-- Changes:
-- 11Nov2007 Clive Convert to SQL2005 Operation (n.b. this cannot reside in Master as a result)
-- 24Jun2006 Clive Make VB/C# switchable
-- 09Oct2005 Clive Update documentation
-- 01Sep2005 Clive Extend to cover more data types
-- 09Jun2005 Clive Make c# variant of vb.net
-- 14Feb2005 Clive Autogenerate data access code
-- 14Oct2000 Clive Tidied output
-- 01Aug2000 Clive Documentation update
-- 31Jul2000 Clive Modified to put more of documentation header
SET NOCOUNT ON
DECLARE @ColName VARCHAR(80)
DECLARE @COL_NAME VARCHAR(80)
DECLARE @ColLength INT
DECLARE @HasIdent INT
DECLARE @HasTSTAMP INT
DECLARE @RowCount INT
DECLARE @RowIndex INT
DECLARE @PrintLine VARCHAR(256)
DECLARE @SpName VARCHAR(80)
DECLARE @DateWork VARCHAR(16)
DECLARE @TN VARCHAR(80)
DECLARE @USER VARCHAR(80)
DECLARE @colid INT
DECLARE @xprec INT
DECLARE @xscale INT
DECLARE @DOM_NAME VARCHAR(50)
DECLARE @SpPrefix VARCHAR(10)
DECLARE @SET_DB VARCHAR(50)
DECLARE @ObPrefix VARCHAR(10)
DECLARE @DefLanguage CHAR(2)
DECLARE @Separator2 VARCHAR(1)
DECLARE @SqlDbType VARCHAR(20)
DECLARE @COMMENT VARCHAR(128)
DECLARE @ALLOWED BIT
SELECT @ALLOWED = 0
SELECT @LANG = RTRIM(UPPER(LTRIM(@LANG)))
IF @LANG = 'VB' SELECT @ALLOWED = 1
IF @LANG = 'C#' SELECT @ALLOWED = 1
-- Configuration start
SELECT @SpPrefix = 'usp_' -- Set to desired prefix for stored procedures
SELECT @DOM_NAME = 'DALETH' + '\' -- Set to your domain name
-- Set @SET_DB to name of SP to set permissions or to blank
SELECT @SET_DB = 'asp_dbPermissions'
SELECT @ObPrefix = 'x' -- Prefix for object variables
SELECT @DefLanguage = 'VB' -- Default language C# or VB
SELECT @Separator2 = '' -- Separator between Tablename and action
-- Configuration end
IF @ALLOWED = 0 SET @LANG = @DefLanguage
IF @TABLENAME ='xyzzy'
OR (NOT EXISTS (SELECT * FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'))
BEGIN
PRINT '--Use csp_MakeUpdate TABLENAME (XX) to generate update '
PRINT 'S.P. for TABLENAME and XX is language for Wrapper VB or C#'
PRINT '--or csp_MakeUpdate garbage to get this help text!'
PRINT 'GO'
RETURN
END
SELECT @TN = name FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'
DECLARE @DocTable TABLE
(
Comment VARCHAR(128),
Colname VARCHAR(128),
Coltype VARCHAR(128),
Cstatus INT,
ColId INT,
Cid INT,
length INT,
xprec INT,
xscale INT
)
INSERT INTO @DocTable (Comment, Colname, Coltype, Cstatus,
ColId, Cid, length, xprec, xscale)
SELECT COALESCE(CONVERT(VARCHAR(130),P.value),
'[Undocumented in Database]') AS Comment,
C.name AS Colname, UPPER(U.name) AS Coltype, C.status AS Cstatus,
C.ColId, C.Id AS Cid, C.length, C.xprec, C.xscale
FROM syscolumns C
JOIN sys.tables O ON C.id = O.object_id
JOIN systypes U ON C.xusertype = U.xusertype
LEFT OUTER JOIN SYS.extended_properties P ON P.minor_id = C.COLID AND C.id = P.major_id
WHERE O.name = @TN AND U.name <> 'timestamp'
ORDER BY C.colid
SELECT @HasTSTAMP = (SELECT COUNT(*) FROM @DocTable WHERE Colname =
-- Begin updateable
'UpdateCount')
-- End Updateable
SELECT @SpName = @SpPrefix + RTRIM(@TN) + @Separator2 + 'Update'
SELECT @ColLength = MAX(DATALENGTH(RTRIM(CONVERT(VARCHAR(80),Colname))))
FROM @DocTable
WHERE Cstatus <> 128
IF @ColLength < 14 SET @ColLength = 14
IF EXISTS(select * from sysobjects
WHERE UPPER(name) = UPPER(@SpName))
SELECT @PrintLine='ALTER PROCEDURE dbo.' + @SpName
ELSE
SELECT @PrintLine='CREATE PROCEDURE dbo.' + @SpName
----
PRINT @PrintLine
PRINT '('
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Coltype <> 'timestamp' AND Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate','UpdateDate')
-- End Updateable
SELECT @HasIdent=COUNT(*) FROM @DocTable WHERE Cstatus=128
DECLARE xArgs CURSOR FOR
SELECT '@' + Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Coltype <> 'timestamp' AND Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate', 'UpdateDate')
-- End Updateable
ORDER BY colid
DECLARE @ArgName VARCHAR(80),
@Type VARCHAR(32),
@Length INT
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @ArgName, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
SELECT @ArgName = SUBSTRING(@ArgName
+ ' ',1,@ColLength + 4)
SELECT @PrintLine = ' ' + @ArgName + @Type
IF @Type='char'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length)) + ')'
IF @Type='varchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length)) + ')'
IF @Type='nchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length/2)) + ')'
IF @Type='nvarchar'
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@Length/2)) + ')'
IF (@Type='decimal') OR (@Type='numeric')
SELECT @PrintLine=@PrintLine + ' ('
+ RTRIM(convert(varchar(10),@xprec))
+ ',' + RTRIM(convert(varchar(10),@xscale)) + ')'
SELECT @PrintLine = @PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
PRINT ' @Return INT OUTPUT'
PRINT ') AS'
PRINT '-- Purpose:'
SELECT @PrintLine='-- Update record on ' + @TN + ' table'
PRINT @PrintLine
PRINT '-- Parameters:'
DECLARE xArgs CURSOR FOR
SELECT Colname, Comment FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId', 'CreateDate','UpdateDate')
-- End Updateable
ORDER BY colid
OPEN xArgs
FETCH xArgs INTO @ArgName, @COMMENT
WHILE @@FETCH_status=0
BEGIN
SELECT @ArgName=SUBSTRING(@ArgName
+ ' ',1,@ColLength + 2)
SELECT @PrintLine='-- ' + @ArgName + '- ' + @COMMENT
PRINT @PrintLine
FETCH xArgs INTO @ArgName, @COMMENT
END
CLOSE xArgs
DEALLOCATE xArgs
PRINT '-- Return - Return value'
PRINT '-- History: '
SELECT @USER = system_user
IF SUBSTRING(@USER,1,DATALENGTH(@DOM_NAME)) = @DOM_NAME BEGIN
SELECT @USER = RTRIM(SUBSTRING(@USER,DATALENGTH(@DOM_NAME) + 1,20))
END
SELECT @DateWork = CONVERT(CHAR(11),CURRENT_TIMESTAMP,106)
SELECT @DateWork = substring(@DateWork,1,2)
+ SUBSTRING(@DateWork,4,3) + SUBSTRING(@DateWork,8,4)
SELECT @PrintLine = '-- ' + @DateWork + ' '
SELECT @PrintLine = @PrintLine + @USER + ' Original coding'
PRINT @PrintLine
PRINT 'SET NOCOUNT ON'
IF @HasIdent > 0 AND (@HasTSTAMP > 0) BEGIN
SELECT @PrintLine='IF EXISTS(SELECT * FROM ' + @TN + '
WHERE Id = @Id AND UpdateCount = @UpdateCount) BEGIN '
PRINT @PrintLine
END
SELECT @PrintLine=' UPDATE ' + @TN + ' SET'
PRINT @PrintLine
DECLARE @SetValue VARCHAR(80)
SELECT @RowCount = COUNT(*) FROM @DocTable
WHERE Colname NOT IN ('CreatePersonId','CreateDate')
AND Cstatus <> 128
DECLARE xArgs CURSOR FOR
SELECT Colname FROM @DocTable
WHERE Colname NOT IN ('CreatePersonId','CreateDate')
AND Cstatus <> 128
ORDER BY colid
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @ArgName
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex=@RowIndex + 1
SELECT @ArgName=SUBSTRING(@ArgName
+ ' ',1,@ColLength+2)
IF @ArgName = 'UpdateDate ' BEGIN
SELECT @PrintLine = ' ' + @ArgName + '= GETUTCDATE()'
END
ELSE BEGIN
IF @ArgName = 'UpdateCount' BEGIN
SELECT @PrintLine = ' ' + @ArgName
+ ' = UpdateCount + 1'
END
ELSE BEGIN
SELECT @PrintLine = ' ' + @ArgName
+ ' = @' + RTRIM(@ArgName)
END
END
IF @RowIndex <> @RowCount
SELECT @PrintLine=@PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName
END
CLOSE xArgs
DEALLOCATE xArgs
SELECT @RowCount = COUNT(*) FROM @DocTable
-- There is a missing where clause here!
IF @RowCount = 0 BEGIN
PRINT '-- You MUST amend the following line to '
+ 'correctly identify the row to update'
PRINT 'WHERE SOMECOLUMN = @SOMEVALUE'
PRINT ''
PRINT 'SELECT @Return = @@error'
PRINT 'RETURN'
END
ELSE BEGIN
IF (@HasIdent > 0) AND (@HasTSTAMP > 0) BEGIN
SELECT @PrintLine
=' WHERE Id = @Id AND UpdateCount = @UpdateCount '
PRINT @PrintLine
PRINT ' SELECT @Return = @@error'
PRINT ' RETURN'
PRINT ' END '
PRINT 'ELSE '
PRINT ' SELECT @Return = 13'
PRINT ' RETURN'
END
ELSE BEGIN
PRINT 'WHERE '
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Cstatus=128
DECLARE xArgs CURSOR FOR
SELECT Colname + ' = @' + Colname
FROM @DocTable
WHERE Cstatus=128
ORDER BY Colid
SELECT @RowIndex=0
OPEN xArgs
FETCH xArgs INTO @SetValue
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
SELECT @PrintLine = ' ' + @SetValue
IF @RowIndex <> @RowCount
SELECT @PrintLine = @PrintLine + ' AND'
PRINT @PrintLine
FETCH xArgs INTO @SetValue
END
CLOSE xArgs
DEALLOCATE xArgs
IF @HasTSTAMP = 1 BEGIN
PRINT ' AND UpdateCount = @UpdateCount'
------
PRINT ''
END
PRINT 'SELECT @Return = @@error'
PRINT 'RETURN'
END
END
PRINT '-------------- this is the end ----------------'
PRINT '/* Data Access code follows:'
SELECT @RowIndex = 0
IF @LANG = 'VB' BEGIN
PRINT ' #Region " Update "'
PRINT ' '''''' <summary> '
PRINT ' '''''' Update record on ' + @TN + ' table '
PRINT ' '''''' </summary>'
PRINT ' '''''' <remarks>'
PRINT ' '''''' This code was autogenerated on ' + @DateWork
PRINT ' '''''' </remarks>'
END
ELSE BEGIN
PRINT ' #region " Update "'
PRINT ' /// <summary> '
PRINT ' /// Update record on ' + @TN + ' table '
PRINT ' /// </summary>'
PRINT ' /// <remarks>'
PRINT ' /// This code was autogenerated on ' + @DateWork
PRINT ' /// </remarks>'
END
DECLARE xArgs CURSOR FOR -- need comment fetch
SELECT Comment, Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId
OPEN xArgs
FETCH xArgs INTO @COMMENT, @COL_NAME, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
IF @LANG = 'VB'
SELECT @PrintLine = ' '''''' <param name="'
ELSE
SELECT @PrintLine = ' /// <param name="'
SELECT @SqlDbType = 'x' -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'l'
IF @Type='bit' SELECT @SqlDbType = 'b'
IF @Type='int' SELECT @SqlDbType = 'i'
IF @Type='decimal' SELECT @SqlDbType = 'c'
IF @Type='smallmoney' SELECT @SqlDbType = 'c'
IF @Type='numeric' SELECT @SqlDbType = 'c'
IF @Type='datetime' SELECT @SqlDbType = 'dat'
IF @Type='smalldatetime' SELECT @SqlDbType = 'dat'
IF @Type='char' SELECT @SqlDbType = 's'
IF @Type='varchar' SELECT @SqlDbType = 's'
IF @Type='nchar' SELECT @SqlDbType = 's'
IF @Type='nvarchar' SELECT @SqlDbType = 's'
IF @Type='text' SELECT @SqlDbType = 's'
IF @Type='ntext' SELECT @SqlDbType = 's'
--IF @COL_NAME = 'UpdatePersonId'
-- SELECT @COMMENT = 'Id of user from GetUserPersonId'
--IF @COL_NAME = 'UpdateCount'
-- SELECT @COMMENT = 'Count of existing updates to row'
-- More comment signs here ?
SELECT @PrintLine = @PrintLine + @SqlDbType + @COL_NAME
+ '">' + @COMMENT + '</param>'
PRINT @PrintLine
FETCH xArgs INTO @COMMENT, @COL_NAME, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' '''''' <param name="iReturn">Return code to '
+ 'use in switch statement</param>'
PRINT ' Public Sub Update' + @TN + '(iId As Integer, _'
END
ELSE BEGIN
PRINT ' /// <param name="iReturn">Return code to use'
+ ' in switch statement</param>'
PRINT ' public void Update' + @TN + '(int iId,'
END
SELECT @RowCount=COUNT(*) FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
DECLARE @PREFIX VARCHAR(10)
DECLARE xArgs CURSOR FOR
SELECT Colname, Coltype, length, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId
OPEN xArgs
FETCH xArgs INTO @COL_NAME, @Type, @Length, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowIndex = @RowIndex + 1
IF @RowIndex > 1 BEGIN
SELECT @SqlDbType = @Type -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'Int64'
IF @Type='smallint' SELECT @SqlDbType = 'Int16'
IF @Type='bit' SELECT @SqlDbType = 'bool'
IF @Type='int' SELECT @SqlDbType = 'int'
IF @Type='decimal' SELECT @SqlDbType = 'decimal'
IF @Type='numeric' SELECT @SqlDbType = 'decimal'
IF @Type='datetime' SELECT @SqlDbType = 'DateTime'
IF @Type='smalldatetime' SELECT @SqlDbType = 'DateTime'
IF @Type='char' SELECT @SqlDbType = 'string'
IF @Type='varchar' SELECT @SqlDbType = 'string'
IF @Type='nchar' SELECT @SqlDbType = 'string'
IF @Type='nvarchar' SELECT @SqlDbType = 'string'
IF @Type='text' SELECT @SqlDbType = 'string'
IF @Type='ntext' SELECT @SqlDbType = 'string'
IF @Type='smallmoney' SELECT @SqlDbType = 'decimal'
IF @Type='money' SELECT @SqlDbType = 'decimal'
IF @Type='real' SELECT @SqlDbType = 'double'
IF @Type='float' SELECT @SqlDbType = 'double'
SELECT @PREFIX = 'x' -- Default assignment
IF @Type='bigint' SELECT @PREFIX = 'l'
IF @Type='bit' SELECT @PREFIX = 'b'
IF @Type='int' SELECT @PREFIX = 'i'
IF @Type='decimal' SELECT @PREFIX = 'c'
IF @Type='numeric' SELECT @PREFIX = 'c'
IF @Type='datetime' SELECT @PREFIX = 'dat'
IF @Type='smalldatetime' SELECT @PREFIX = 'dat'
IF @Type='char' SELECT @PREFIX = 's'
IF @Type='varchar' SELECT @PREFIX = 's'
IF @Type='nchar' SELECT @PREFIX = 's'
IF @Type='nvarchar' SELECT @PREFIX = 's'
IF @Type='text' SELECT @PREFIX = 's'
IF @Type='ntext' SELECT @PREFIX = 's'
IF @Type='smallmoney' SELECT @PREFIX = 'c'
IF @Type='money' SELECT @PREFIX = 'c'
IF @Type='real' SELECT @PREFIX = 'd'
IF @Type='float' SELECT @PREFIX = 'd'
SELECT @PrintLine = ' '
IF @LANG = 'VB' BEGIN
IF @SqlDbType = 'int' SELECT @SqlDbType = 'Integer'
IF @SqlDbType = 'string' SELECT @SqlDbType = 'String'
IF @SqlDbType = 'decimal' SELECT @SqlDbType = 'Decimal'
IF @SqlDbType = 'bit' SELECT @SqlDbType = 'Boolean'
IF @SqlDbType = 'bool' SELECT @SqlDbType = 'Boolean'
SELECT @PrintLine = @PrintLine
+ @PREFIX + @COL_NAME + ' As '
SELECT @PrintLine = @PrintLine + @SqlDbType -- + ' '
END
ELSE BEGIN
SELECT @PrintLine = @PrintLine + @SqlDbType + ' '
SELECT @PrintLine = @PrintLine + @PREFIX + @COL_NAME
END
IF @RowIndex = @RowCount
IF @LANG = 'VB'
SELECT @PrintLine = @PrintLine
+ ', ByRef iReturn As DatabaseReturnCode)'
ELSE
SELECT @PrintLine = @PrintLine
+ ', ref DatabaseReturnCode iReturn)'
ELSE BEGIN
SELECT @PrintLine = @PrintLine + ', '
IF @LANG = 'VB' SELECT @PrintLine = @PrintLine + ' _ '
END
PRINT @PrintLine
END
FETCH xArgs INTO @COL_NAME, @Type, @Length, @xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' iReturn = DatabaseReturnCode.NoErrorDetected'
PRINT ' Dim sConnect As String = CommonData.GetConnect()'
PRINT ' Dim ' + @ObPrefix
+ 'SqlConnection As SqlConnection = new SqlConnection(sConnect)'
PRINT ' Dim ' + @ObPrefix
+ 'SqlCommand As SqlCommand = New SqlCommand("'
+ @SpName + '", ' + @ObPrefix + 'SqlConnection)'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.CommandType = CommandType.StoredProcedure'
PRINT ' Try'
END
ELSE BEGIN
PRINT ' {'
PRINT ' iReturn = DatabaseReturnCode.NoErrorDetected;'
PRINT ' string sConnect = CommonData.GetConnect();'
PRINT ' SqlConnection ' + @ObPrefix
+ 'SqlConnection = new SqlConnection(sConnect);'
PRINT ' SqlCommand ' + @ObPrefix
+ 'SqlCommand = new SqlCommand("'
+ @SpName + '", ' + @ObPrefix + 'SqlConnection);'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.CommandType = CommandType.StoredProcedure;'
PRINT ' try'
PRINT ' {'
END
SELECT @RowIndex = 0
DECLARE xArgs CURSOR FOR
SELECT DISTINCT Colname, Coltype, length, ColId, xprec, xscale
FROM @DocTable
WHERE Colname NOT IN
-- Begin updateable
('REPLICASTAMP','CreatePersonId','CreateDate','UpdateDate')
-- End Updateable
ORDER BY ColId, Colname
OPEN xArgs
FETCH xArgs INTO @COL_NAME, @Type, @Length, @colid, @xprec, @xscale
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@'
+ @COL_NAME + '", SqlDbType.'
SELECT @SqlDbType = @Type -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'BigInt'
IF @Type='bit' SELECT @SqlDbType = 'Bit'
IF @Type='int' SELECT @SqlDbType = 'Int'
IF @Type='decimal' SELECT @SqlDbType = 'Decimal'
IF @Type='numeric' SELECT @SqlDbType = 'Decimal'
IF @Type='datetime' SELECT @SqlDbType = 'DateTime'
IF @Type='char' SELECT @SqlDbType = 'Char'
IF @Type='varchar' SELECT @SqlDbType = 'VarChar'
IF @Type='nchar' SELECT @SqlDbType = 'NChar'
IF @Type='nvarchar' SELECT @SqlDbType = 'NVarChar'
IF @Type='ntext' SELECT @SqlDbType = 'NText'
IF @Type='text' SELECT @SqlDbType = 'Text'
IF @Type='real' SELECT @SqlDbType = 'Real'
IF @Type='smalldatetime' SELECT @SqlDbType = 'SmallDateTime'
IF @Type='smallint' SELECT @SqlDbType = 'SmallInt'
IF @Type='smallmoney' SELECT @SqlDbType = 'SmallMoney'
-------------
IF @Type='float' SELECT @SqlDbType = 'Float'
-- more type conversions here !
SELECT @PrintLine = @PrintLine + @SqlDbType
IF @Type='char' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='varchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='nchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @Type='nvarchar' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @Type='text' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length))
IF @Type='ntext' SELECT @PrintLine=@PrintLine + ', '
+ RTRIM(convert(varchar,@Length/2))
IF @LANG = 'C#'
SELECT @PrintLine=@PrintLine + ');'
ELSE
SELECT @PrintLine=@PrintLine + ')'
PRINT @PrintLine
IF (@Type='decimal') OR (@Type='numeric') BEGIN
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Precision = '
+ CONVERT(VARCHAR(3), @xprec)
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Precision = '
+ CONVERT(VARCHAR(3), @xprec) + ';'
PRINT @PrintLine
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Scale = '
+ CONVERT(VARCHAR(3), @xscale)
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Scale = '
+ CONVERT(VARCHAR(3), @xscale) + ';'
PRINT @PrintLine
END
SELECT @RowIndex=@RowIndex + 1
IF @RowIndex = 1
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Value=iId'
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Value=iId;'
ELSE BEGIN
IF @LANG = 'VB'
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters("@'
+ @COL_NAME + '").Value = '
ELSE
SELECT @PrintLine=' ' + @ObPrefix
+ 'SqlCommand.Parameters["@'
+ @COL_NAME + '"].Value = '
SELECT @SqlDbType = 'x' -- Default assignment
IF @Type='bigint' SELECT @SqlDbType = 'l'
IF @Type='bit' SELECT @SqlDbType = 'b'
IF @Type='int' SELECT @SqlDbType = 'i'
IF @Type='decimal' SELECT @SqlDbType = 'c'
IF @Type='numeric' SELECT @SqlDbType = 'c'
IF @Type='datetime' SELECT @SqlDbType = 'dat'
IF @Type='smalldatetime' SELECT @SqlDbType = 'dat'
IF @Type='char' SELECT @SqlDbType = 's'
IF @Type='varchar' SELECT @SqlDbType = 's'
IF @Type='nchar' SELECT @SqlDbType = 's'
IF @Type='nvarchar' SELECT @SqlDbType = 's'
IF @Type='text' SELECT @SqlDbType = 's'
IF @Type='ntext' SELECT @SqlDbType = 's'
IF @Type='smallmoney' SELECT @SqlDbType = 'c'
IF @Type='money' SELECT @SqlDbType = 'c'
IF @SqlDbType = 'c' BEGIN
SELECT @PrintLine = @PrintLine + '' + @ObPrefix
+ 'CommonData.DecimalAdjust('
END
SELECT @PrintLine = @PrintLine + @SqlDbType + @COL_NAME
IF @SqlDbType = 'c' BEGIN
SELECT @PrintLine = @PrintLine + ', '
+ RTRIM(convert(varchar(10),@xscale)) + ')'
END
END
IF @LANG = 'VB'
PRINT @PrintLine
ELSE
PRINT @PrintLine + ';'
FETCH xArgs INTO @COL_NAME, @Type, @Length, @colid,
@xprec, @xscale
END
CLOSE xArgs
DEALLOCATE xArgs
IF @LANG = 'VB' BEGIN
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@Return", SqlDbType.Int)'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters("@Return").Direction '
+ '= ParameterDirection.Output'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Open()'
PRINT ' ' + @ObPrefix + 'SqlCommand.ExecuteNonQuery()'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close()'
-- iReturn = Ctype(CommonData.NullToInteger(xSqlCommand.Parameters("@Return").Value),DatabaseReturnCode)
PRINT ' iReturn = Ctype(CommonData.NullToInteger(' + @ObPrefix
+ 'SqlCommand.Parameters("@Return").Value), DatabaseReturnCode)'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose()'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose()'
END
ELSE BEGIN
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters.Add("@Return", SqlDbType.Int);'
PRINT ' ' + @ObPrefix
+ 'SqlCommand.Parameters["@Return"].Direction = ParameterDirection.Output;'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Open();'
PRINT ' ' + @ObPrefix + 'SqlCommand.ExecuteNonQuery();'
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close();'
PRINT ' iReturn = (DatabaseReturnCode)CommonData.NullToInteger(' + @ObPrefix
+ 'SqlCommand.Parameters["@Return"].Value);'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose();'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose();'
PRINT ' }'
END
IF @LANG = 'VB' BEGIN
PRINT ' Catch ex As System.InvalidOperationException'
PRINT ' Dim sMethod As String = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString()'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "")'
PRINT ' iReturn = DatabaseReturnCode.NetworkError'
PRINT ' Catch ex As Exception'
PRINT ' Dim sMethod As String = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString()'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "iId="'
+ ' & iId.ToString())'
PRINT ' iReturn = DatabaseReturnCode.OtherError'
PRINT ' Finally '
PRINT ' ' + @ObPrefix + 'SqlCommand.Connection.Close()'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose()'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose()'
PRINT ' End Try '
PRINT ' End Sub'
PRINT ' #End Region'
END
ELSE BEGIN
PRINT ' catch (System.InvalidOperationException ex)'
PRINT ' {'
PRINT ' string sMethod = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name;'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "");'
PRINT ' iReturn = DatabaseReturnCode.NetworkError;'
PRINT ' }'
PRINT ' catch (Exception ex)'
PRINT ' {'
PRINT ' string sMethod = '
+ 'System.Reflection.MethodInfo.GetCurrentMethod().Name;'
PRINT ' CommonData.WriteEventError(MODULE_NAME, sMethod, ex, "");'
PRINT ' iReturn = DatabaseReturnCode.OtherError;'
PRINT ' }'
PRINT ' finally '
PRINT ' {'
PRINT ' if (' + @ObPrefix + 'SqlCommand.Connection != null) '
+ @ObPrefix + 'SqlCommand.Connection.Close();'
PRINT ' ' + @ObPrefix + 'SqlCommand.Dispose();'
PRINT ' ' + @ObPrefix + 'SqlConnection.Dispose();'
PRINT ' }'
PRINT ' }'
PRINT ' #endregion'
END
PRINT '*/'
PRINT 'GO'
PRINT @SET_DB
PRINT 'GO'
RETURN
-------------- this is the end ----------------
GO
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239