CREATE Procedure [dbo].[csp_MakeListAll]
(
@TABLENAME VARCHAR(255)= 'xyzzy',
@LANG VARCHAR(2) = 'VB' -- VB or C# (default set in configration section)
) AS
-- Purpose:
-- Create List script from table definition
-- Copyright (C) 2000, 2003, 2004, 2006, 2007, 2008 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 any database on this server by entering
-- sp_MakeSelect TABLENAME XX
-- where XX is either VB or C#
-- 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
-- 14Oct2000 Clive - Original coding
DECLARE @ColName VARCHAR(32)
DECLARE @ColLength INT
DECLARE @COUNT_ROW INT
DECLARE @HasIdent INT
DECLARE @RowCount INT
DECLARE @RowIndex INT
DECLARE @PrintLine VARCHAR(132)
DECLARE @SpName VARCHAR(32)
DECLARE @SpUpper VARCHAR(32)
DECLARE @DateWork VARCHAR(16)
DECLARE @TN VARCHAR(32)
DECLARE @SetValue VARCHAR(80)
DECLARE @USER VARCHAR(32)
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 @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'
GOTO HelpText
ELSE
BEGIN
IF NOT EXISTS (SELECT * FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U')
GOTO HelpText
ELSE BEGIN
SELECT @TN = name FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'
SELECT @RowCount=0
DECLARE xArgs CURSOR FOR
SELECT C.name FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
ORDER BY C.colid
OPEN xArgs
FETCH xArgs INTO @ColName
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowCount=@RowCount+1
FETCH xArgs INTO @ColName
END
CLOSE xArgs
DEALLOCATE xArgs
-- Change next line to change stored procedure name
SELECT @SpName = @SpPrefix + RTRIM(@TN) + '_ListAll'
-- Configuration end
SELECT @ColLength= MAX(DATALENGTH(C.name))
FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN AND C.status<>128
SELECT @SpUpper = UPPER(@SpName)
IF EXISTS(select * from sysobjects
where (UPPER(name) = @SpUpper) AND (sysstat & 0xf = 4))
SELECT @PrintLine='ALTER PROCEDURE dbo.' + @SpName + ' AS'
ELSE
SELECT @PrintLine='CREATE PROCEDURE dbo.' + @SpName + ' AS'
PRINT @PrintLine
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.status=128
SELECT @HasIdent=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN AND C.status=128
DECLARE @ArgName VARCHAR(36),
@Type VARCHAR(32),
@Length VARCHAR(40)
SELECT @RowIndex=0
PRINT '-- Purpose:'
SELECT @PrintLine='-- Select list of all on ' + @TN + ' table'
PRINT @PrintLine
--PRINT '-- Resultset:'
--PRINT '-- '
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'
SELECT @PrintLine='SELECT '
PRINT @PrintLine
SELECT @RowCount=COUNT(*)
FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.NAME NOT IN
('ADDDATE','MODDATE','DELDATE','TSTAMP','LAST_PERSON_ID')
DECLARE xArgs CURSOR FOR
SELECT C.name FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.NAME NOT IN
('ADDDATE','MODDATE','DELDATE','TSTAMP','LAST_PERSON_ID')
ORDER BY C.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)
SELECT @PrintLine = ' ' + RTRIM(@ArgName)
IF @RowIndex <> @RowCount
SELECT @PrintLine=@PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName
END
CLOSE xArgs
DEALLOCATE xArgs
SELECT @PrintLine=' FROM ' + @TN
PRINT @PrintLine
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
IF @RowCount = 0 BEGIN
PRINT '-- You MUST amend the following line to correctly identify the row to order by'
PRINT 'ORDER BY SOMECOLUMN'
END
ELSE BEGIN
PRINT 'ORDER BY '
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
AND C.status=128
DECLARE xArgs CURSOR FOR
SELECT C.name
FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN
AND C.xusertype=U.xusertype AND C.status=128
ORDER BY C.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
END
END
END
PRINT 'RETURN'
PRINT '----------------- this is the end --------------'
PRINT '/* Data Access code follows:'
IF @LANG = 'VB' BEGIN
PRINT ' #Region " List "'
PRINT ' '''''' <summary> '
PRINT ' '''''' Select list of all on ' + @TN + ' table '
PRINT ' '''''' </summary>'
PRINT ' '''''' <remarks>'
PRINT ' '''''' This code was autogenerated on ' + @DateWork
PRINT ' '''''' </remarks>'
---------
PRINT ' '''''' <returns>dataset of ' + @TN + ' </returns>'
PRINT ' Public Function List' + @TN + 'All() As DataSet '
PRINT ' Return CommonData.GetDataset("' + @SpName + '")'
PRINT ' End Function'
PRINT ' #End Region'
END
ELSE BEGIN
PRINT ' #region " List "'
PRINT ' /// <summary> '
PRINT ' /// Select list of all on ' + @TN + ' table '
PRINT ' /// </summary>'
PRINT ' /// <remarks>'
PRINT ' /// This code was autogenerated on ' + @DateWork
PRINT ' /// </remarks>'
PRINT ' /// <returns>dataset of ' + @TN + ' </returns>'
PRINT ' public DataSet List' + @TN + 'All()'
PRINT ' {'
PRINT ' return CommonData.GetDataset("' + @SpName + '");'
PRINT ' }'
PRINT ' #endregion'
END
PRINT '*/'
PRINT 'GO'
PRINT @SET_DB
PRINT 'GO'
RETURN
HelpText:
PRINT '--Use sp_MakeListAll 'TABLENAME', 'XX' to generate select S.P. for TABLENAME'
PRINT ' and XX is the language for the Wrapper VB or C#'
PRINT '--or sp_MakeListAll garbage to get this help text!'
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
72415 Points
14017 Posts
MVP
Re: filling the textbox,dropdownlist from the database??
Mar 08, 2008 02:54 PM|LINK
The script that produced it was:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE Procedure [dbo].[csp_MakeListAll]
(
@TABLENAME VARCHAR(255)= 'xyzzy',
@LANG VARCHAR(2) = 'VB' -- VB or C# (default set in configration section)
) AS
-- Purpose:
-- Create List script from table definition
-- Copyright (C) 2000, 2003, 2004, 2006, 2007, 2008 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 any database on this server by entering
-- sp_MakeSelect TABLENAME XX
-- where XX is either VB or C#
-- 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
-- 14Oct2000 Clive - Original coding
DECLARE @ColName VARCHAR(32)
DECLARE @ColLength INT
DECLARE @COUNT_ROW INT
DECLARE @HasIdent INT
DECLARE @RowCount INT
DECLARE @RowIndex INT
DECLARE @PrintLine VARCHAR(132)
DECLARE @SpName VARCHAR(32)
DECLARE @SpUpper VARCHAR(32)
DECLARE @DateWork VARCHAR(16)
DECLARE @TN VARCHAR(32)
DECLARE @SetValue VARCHAR(80)
DECLARE @USER VARCHAR(32)
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 @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'
GOTO HelpText
ELSE
BEGIN
IF NOT EXISTS (SELECT * FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U')
GOTO HelpText
ELSE BEGIN
SELECT @TN = name FROM sysobjects
WHERE UPPER(name)=UPPER(@TABLENAME) and type='U'
SELECT @RowCount=0
DECLARE xArgs CURSOR FOR
SELECT C.name FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
ORDER BY C.colid
OPEN xArgs
FETCH xArgs INTO @ColName
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @RowCount=@RowCount+1
FETCH xArgs INTO @ColName
END
CLOSE xArgs
DEALLOCATE xArgs
-- Change next line to change stored procedure name
SELECT @SpName = @SpPrefix + RTRIM(@TN) + '_ListAll'
-- Configuration end
SELECT @ColLength= MAX(DATALENGTH(C.name))
FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN AND C.status<>128
SELECT @SpUpper = UPPER(@SpName)
IF EXISTS(select * from sysobjects
where (UPPER(name) = @SpUpper) AND (sysstat & 0xf = 4))
SELECT @PrintLine='ALTER PROCEDURE dbo.' + @SpName + ' AS'
ELSE
SELECT @PrintLine='CREATE PROCEDURE dbo.' + @SpName + ' AS'
PRINT @PrintLine
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.status=128
SELECT @HasIdent=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN AND C.status=128
DECLARE @ArgName VARCHAR(36),
@Type VARCHAR(32),
@Length VARCHAR(40)
SELECT @RowIndex=0
PRINT '-- Purpose:'
SELECT @PrintLine='-- Select list of all on ' + @TN + ' table'
PRINT @PrintLine
--PRINT '-- Resultset:'
--PRINT '-- '
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'
SELECT @PrintLine='SELECT '
PRINT @PrintLine
SELECT @RowCount=COUNT(*)
FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.NAME NOT IN
('ADDDATE','MODDATE','DELDATE','TSTAMP','LAST_PERSON_ID')
DECLARE xArgs CURSOR FOR
SELECT C.name FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN AND C.xusertype=U.xusertype
AND U.name<>'timestamp' AND C.NAME NOT IN
('ADDDATE','MODDATE','DELDATE','TSTAMP','LAST_PERSON_ID')
ORDER BY C.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)
SELECT @PrintLine = ' ' + RTRIM(@ArgName)
IF @RowIndex <> @RowCount
SELECT @PrintLine=@PrintLine + ','
PRINT @PrintLine
FETCH xArgs INTO @ArgName
END
CLOSE xArgs
DEALLOCATE xArgs
SELECT @PrintLine=' FROM ' + @TN
PRINT @PrintLine
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
IF @RowCount = 0 BEGIN
PRINT '-- You MUST amend the following line to correctly identify the row to order by'
PRINT 'ORDER BY SOMECOLUMN'
END
ELSE BEGIN
PRINT 'ORDER BY '
SELECT @RowCount=COUNT(*) FROM syscolumns C, sysobjects O
WHERE C.id = O.id AND O.name = @TN
AND C.status=128
DECLARE xArgs CURSOR FOR
SELECT C.name
FROM syscolumns C, sysobjects O, systypes U
WHERE C.id = O.id AND O.name = @TN
AND C.xusertype=U.xusertype AND C.status=128
ORDER BY C.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
END
END
END
PRINT 'RETURN'
PRINT '----------------- this is the end --------------'
PRINT '/* Data Access code follows:'
IF @LANG = 'VB' BEGIN
PRINT ' #Region " List "'
PRINT ' '''''' <summary> '
PRINT ' '''''' Select list of all on ' + @TN + ' table '
PRINT ' '''''' </summary>'
PRINT ' '''''' <remarks>'
PRINT ' '''''' This code was autogenerated on ' + @DateWork
PRINT ' '''''' </remarks>'
---------
PRINT ' '''''' <returns>dataset of ' + @TN + ' </returns>'
PRINT ' Public Function List' + @TN + 'All() As DataSet '
PRINT ' Return CommonData.GetDataset("' + @SpName + '")'
PRINT ' End Function'
PRINT ' #End Region'
END
ELSE BEGIN
PRINT ' #region " List "'
PRINT ' /// <summary> '
PRINT ' /// Select list of all on ' + @TN + ' table '
PRINT ' /// </summary>'
PRINT ' /// <remarks>'
PRINT ' /// This code was autogenerated on ' + @DateWork
PRINT ' /// </remarks>'
PRINT ' /// <returns>dataset of ' + @TN + ' </returns>'
PRINT ' public DataSet List' + @TN + 'All()'
PRINT ' {'
PRINT ' return CommonData.GetDataset("' + @SpName + '");'
PRINT ' }'
PRINT ' #endregion'
END
PRINT '*/'
PRINT 'GO'
PRINT @SET_DB
PRINT 'GO'
RETURN
HelpText:
PRINT '--Use sp_MakeListAll 'TABLENAME', 'XX' to generate select S.P. for TABLENAME'
PRINT ' and XX is the language for the Wrapper VB or C#'
PRINT '--or sp_MakeListAll garbage to get this help text!'
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