473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

generating DAL

Hi guys,
Is there any free stuff to generate DAL code out there?
Thanks in advance
Aug 28 '06 #1
3 1336
you checked out the enterprise library already? or do you want to generate
more code than what that saves you?
"Patrick.O.Ige" <na********@hotmail.comwrote in message
news:uE**************@TK2MSFTNGP03.phx.gbl...
Hi guys,
Is there any free stuff to generate DAL code out there?
Thanks in advance


Aug 28 '06 #2
"Patrick.O.Ige" <na********@hotmail.comwrote in message
news:uE**************@TK2MSFTNGP03.phx.gbl...
Is there any free stuff to generate DAL code out there?
This is probably the best place to start...
http://msdn.microsoft.com/msdnmag/is...07/DataPoints/
Aug 28 '06 #3
Hope this helps.

This template code generates sample C# Code OR SQL SP for Insert /
Delete / Update / Select, based on the input parameter you pass. The
first param value is SQL Table Name & 2nd param is the type of object
you are interested in creating

Steps + Assumption
===============
1. Ensure you have SQL table created in your DB
2. Create this SP in your DB
3. Execute this SP as
Exec GenerateStoredProcedureTemplate @TableName = '<YourTableName>',
@SPType = 'D'
go

Here are different possible values for 2nd param:

@SPType = 'B' -- Stored Procedure (Generate Business Object)
@SPType = 'I' -- Stored Procedure (Insert Data)
@SPType = 'U' -- Stored Procedure (Update Data)
@SPType = 'D' -- Stored Procedure (Delete Data)
@SPType = 'SA' -- Stored Procedure (SelectAll Data)
@SPType = 'SPK' -- Stored Procedure (SelectUsingPK Data)

Note: I have ensured that there are pointers to URL's in the SP from
where I have copied the original code & then extended it.
/************************************************** ****************************
* File: GenerateStoredProcedureTemplate.SQL
*------------------------------------------------------------------------------
* DATE AUTHOR DESCRIPTION
*------------------------------------------------------------------------------
* 19 Dec 2005 PP New
* 14 Apr 2006 PP Added Logic to Append LTRIM+RTRIM on Insert,
Update SP
Added the logic [Order By ColumnID]
Added Logic to geberate Business Object
File.
*------------------------------------------------------------------------------
************************************************** *****************************/
-------------------------
Set NOCOUNT ON
Set ARITHABORT OFF
Set Quoted_Identifier OFF
Set ANSI_WARNINGS OFF
-------------------------

If Exists ( Select * From sysobjects
Where id = object_id(N'[GenerateStoredProcedureTemplate]')
And OBJECTPROPERTY(id, N'IsProcedure') = 1 )
Begin
Print 'Stored Procedure [GenerateStoredProcedureTemplate]
Dropped Sucessfully!!!'
Drop Procedure [GenerateStoredProcedureTemplate]
End
GO

--------------------------------------------------------------------------------
/*
Exec GenerateStoredProcedureTemplate @TableName = 'Partner', @SPType
= 'B' -- Stored Procedure (Generate Business Object)
Exec GenerateStoredProcedureTemplate @TableName = 'Solution', @SPType
= 'I' -- Stored Procedure (Insert Data)
Exec GenerateStoredProcedureTemplate @TableName = 'Partner', @SPType
= 'U' -- Stored Procedure (Update Data)
Exec GenerateStoredProcedureTemplate @TableName = 'Partner', @SPType
= 'D' -- Stored Procedure (Delete Data)
Exec GenerateStoredProcedureTemplate @TableName = 'Partner', @SPType
= 'SA' -- Stored Procedure (SelectAll Data)
Exec GenerateStoredProcedureTemplate @TableName = 'Partner', @SPType
= 'SPK' -- Stored Procedure (SelectUsingPK Data)
*/
--------------------------------------------------------------------------------
-- TableStructure.sql
-- http://www.sqlservercentral.com/scri...utions/738.asp
-- Get the Table Structure using a simple query
----------------------------------------------------------------
CREATE Procedure GenerateStoredProcedureTemplate
@TableName Varchar(255)
,@SPType Varchar(5) -- I : Insert, U: Update, D : Delete, B :
Business Object
AS
Begin

/************************************************** ****************************
* File: GenerateStoredProcedureTemplate.sql
* PURPOSE: To Generate INSERT/UPDATE/DELETE SP template, based on
current schema
* DATE AUTHOR DESCRIPTION

*------------------------------------------------------------------------------
* 19 Dec 2005 PP Created

************************************************** *****************************/
--------------------------
SET NOCOUNT ON
SET ARITHABORT OFF
SET QUOTED_IDENTIFIER OFF
SET ANSI_WARNINGS OFF
--------------------------

--------------------------
-- Declare Variable(s)
--------------------------
Declare @SPName_Insert Varchar(255)
,@SPName_Update Varchar(255)
,@SPName_Delete Varchar(255)
,@SPName_SelectAll Varchar(255)
,@SPName_SelectUsingPK Varchar(255)
,@Action_Insert Varchar(255)
,@Action_Update Varchar(255)
,@Action_Delete Varchar(255)
,@Action_SelectAll Varchar(255)
,@Action_SelectUsingPK Varchar(255)

Declare @ColumnNameMAXLength Int

--------------------------
-- Clean INPUT Variable(s)
--------------------------
Select @TableName = LTRIM(RTRIM(@TableName))
Select @SPType = LTRIM(RTRIM(@SPType))

-----------------------------------
-- Verify Parameter Value Object(s)
-----------------------------------
--Select 'ObjectProperty : ' , ObjectProperty (
Object_ID(@TableName), 'IsTable')
If (ISNULL(ObjectProperty ( Object_ID(@TableName), 'IsTable'),0) !=
1)
Begin
RaisError('Parameter Value for @TableName is not a Valid
Table in this Database', 18,127)
Return -1
End

If (@SPType NOT IN ('I', 'U', 'D', 'B', 'SA', 'SPK'))
Begin
RaisError('Parameter Value for @SPType is not a Valid
value', 18,127)
Return -1
End

---------------------------------------
-- Assign/Initialize Variable(s) values
---------------------------------------
Select @Action_Insert = 'Insert'
,@Action_Update = 'Update'
,@Action_Delete = 'Delete'
,@Action_SelectAll = 'SelectAll'
,@Action_SelectUsingPK = 'SelectUsingPK'

Select @SPName_Insert = @TableName + '_' + @Action_Insert
,@SPName_Update = @TableName + '_' + @Action_Update
,@SPName_Delete = @TableName + '_' + @Action_Delete
,@SPName_SelectAll = @TableName + '_' +
@Action_SelectAll
,@SPName_SelectUsingPK = @TableName + '_' +
@Action_SelectUsingPK

-----------------------------------
-- Identify TABLE column attributes
-----------------------------------
-- Drop table #t
SELECT o.ID TableID,
c.ColID ColumnID,
o.name TableName,
c.name ColumnName,
c.name BOBColumnName,
t.name DataType,
c.Length ColumnLength,
CASE WHEN c.isnullable=1 THEN 'Yes'
ELSE 'No'
END
AS 'Nullable',
COLUMNPROPERTY (Object_ID(@TableName), c.name, 'IsIdentity'
) As 'IdentityColumn'
into #t
FROM sysobjects o
INNER JOIN syscolumns c ON (o.id=c.id and o.type='U' and o.name
not like 'dt%')
Inner Join systypes t ON (c.xtype=t.xtype)
--Where o.Name = @TableName
Where o.Name = @TableName --'Challenges'
Order BY c.colid

--------------------------------------------------
-- Delete columns which are for ADMIN purpose only
--------------------------------------------------
Delete #t where ColumnName IN ('CreatedDate', 'Status',
'UpdatedDate' )
Update #t Set TableName = LTRIM(RTRIM(TableName))
Update #t Set ColumnName = LTRIM(RTRIM(ColumnName))
Update #t Set BOBColumnName = LTRIM(RTRIM(BOBColumnName))

--Select * From #t
--sp_help #t

---------------------------------------
-- Assign/Initialize Variable(s) values
---------------------------------------
Select @ColumnNameMAXLength = MAX(DataLength(ColumnName))
From #t
Select @ColumnNameMAXLength = @ColumnNameMAXLength/2 --
handle nVarchar, so length will be twice
Select @ColumnNameMAXLength = @ColumnNameMAXLength + 5 --
Safety + Beautification

----------------------------------
-- Start [Business Object]
----------------------------------
IF @SPType = 'B'
Begin
Print 'using System;'
Print 'using System.Data;'
Print 'using System.Data.SqlClient;'
Print 'using System.Configuration;'
Print 'using System.Web;'
Print 'using System.Web.Security;'
Print 'using System.Web.UI;'
Print 'using System.Web.UI.WebControls;'
Print 'using System.Web.UI.WebControls.WebParts;'
Print 'using System.Web.UI.HtmlControls;'
Print ''

Print '/// <summary>'
Print '/// Summary description for ' + @TableName
Print '/// </summary>'
Print ''

Print 'public class Event : AlumniLibrary.BaseDataAccess'
Print '{'
Print ' AlumniLibrary.BaseDataAccess objDAL = new
AlumniLibrary.BaseDataAccess();'
Print ''

--------------------------------------------------
-- Replace Special Characters from BOBColumnNames
--------------------------------------------------
Update #t Set BOBColumnName = Replace(BOBColumnName,' ','')
Update #t Set BOBColumnName = Replace(BOBColumnName,'_','')
Update #t Set BOBColumnName = Replace(BOBColumnName,'-','')

--------------------------------------------------
-- Generate c# Code (Variables)
--
http://media.datadirect.com/download....html#wp928843
--------------------------------------------------
Select ' private Int32 int' + BOBColumnName + ';'
>From #t Where DataType In ( 'Int', 'TinyInt', 'smallint' )
UNION
Select ' private Int64 int' + BOBColumnName + ';'
>From #t Where DataType In ( 'BigInt' )
UNION
Select ' private String str' + BOBColumnName + ';'
>From #t Where DataType In ( 'char', 'varchar', 'nchar', 'nvarchar',
'text', 'ntext' )
UNION
Select ' private DateTime dt' + BOBColumnName + ';'
>From #t Where DataType In ( 'datetime', 'smalldatetime' )
UNION
Select ' private Boolean bit' + BOBColumnName + ';'
>From #t Where DataType In ( 'bit' )
UNION
Select ' private Double dbl' + BOBColumnName + ';'
>From #t Where DataType In ( 'float' )
UNION
Select ' private Decimal dcm' + BOBColumnName + ';'
>From #t Where DataType In ( 'Decimal', 'Money', 'Numeric',
'SmallMoney' )
UNION
Select ' private Byte[] byt' + BOBColumnName + ';'
>From #t Where DataType In ( 'Binary', 'Image', 'TimeStamp',
'VarBinary' )
UNION
Select ' private Single sng' + BOBColumnName + ';'
>From #t Where DataType In ( 'Real' )
--------------------------------------------------
-- Generate c# Code (Properties)
--------------------------------------------------
Select ' public Int32 ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return int' + BOBColumnName + ';
}' + Char(10)
+ ' set { int' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'Int', 'TinyInt', 'smallint' )
UNION
Select ' public Int64 ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return int' + BOBColumnName + ';
}' + Char(10)
+ ' set { int' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'BigInt' )
UNION
Select ' public String ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return str' + BOBColumnName + ';
}' + Char(10)
+ ' set { str' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'char', 'varchar', 'nchar',
'nvarchar', 'text', 'ntext' )
UNION
Select ' public DateTime ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return dt' + BOBColumnName + ';
}' + Char(10)
+ ' set { dt' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'datetime', 'smalldatetime' )
UNION
Select ' public Boolean ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return bit' + BOBColumnName + ';
}' + Char(10)
+ ' set { bit' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'bit' )
UNION
Select ' public Double ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return dbl' + BOBColumnName + ';
}' + Char(10)
+ ' set { dbl' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'float' )
UNION
Select ' public Decimal ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return dcm' + BOBColumnName + ';
}' + Char(10)
+ ' set { dcm' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'Decimal', 'Money', 'Numeric',
'SmallMoney' )
UNION
Select ' public Byte[] ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return byt' + BOBColumnName + ';
}' + Char(10)
+ ' set { byt' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'Binary', 'Image', 'TimeStamp',
'VarBinary' )
UNION
Select ' public Single ' + BOBColumnName
+ Char(10)
+ ' {'
+ Char(10)
+ ' get { return sng' + BOBColumnName + ';
}' + Char(10)
+ ' set { sng' + BOBColumnName + ' = value ;
}' + Char(10)
+ ' }'
From #t Where DataType In ( 'Real' )

--------------------------------------------------
-- Generate c# Code (Constructor)
--------------------------------------------------
Select ' public ' + @TableName + '()' +
Char(10)
+ ' {' +
Char(10)
+ ' // TODO: Add constructor logic here' +
Char(10)
+ ' }'
/*
name
--------
sql_variant
uniqueidentifier
*/

Print '}'
End

----------------------------------
-- Start [Insert Stored Procedure]
----------------------------------
IF @SPType = 'I'
Begin
--------------------------------------------
-- <SECTION: Object Existence Check + DROP >
--------------------------------------------
Print 'If Exists ( Select * From SysObjects '
Print ' Where ID = Object_ID(N''[' +
@SPName_Insert + ']'') '
Print ' And OBJECTPROPERTY(id,
N''IsProcedure'') = 1 )'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Insert
+ '] Dropped Sucessfully!!!'''
Print ' Drop Procedure [' + @SPName_Insert + ']'
Print ' End'
Print 'GO'
Print ''

-----------------------------------------
-- <SECTION: Object Creation -- Starting>
-----------------------------------------
Print 'CREATE Procedure ' + @SPName_Insert
--Print ' @TableName Varchar(255)'

Select ' @' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))

+ ' ' + DataType
+ Case WHEN DataType = 'char' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'varchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nvarchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
Else ''
End
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID = 2 -- First NON-Identity column
does not start with a PREFIX ","
--UNION
Select ' ,@' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))

+ ' ' + DataType
+ Case WHEN DataType = 'char' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'varchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nvarchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
Else ''
End
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID 2 -- First NON-Identity column
does not start with a PREFIX ","
Order By ColumnID

Print 'AS'
Print 'Begin'
Print '
/************************************************** ****************************'
Print ' * File : ' + @SPName_Insert
Print ' * PURPOSE : SP to insert/populate Table ' +
@TableName
Print ' * DATE AUTHOR DESCRIPTION'
Print '
*------------------------------------------------------------------------------'
Print ' * ' + Convert(Varchar(12),Getdate()) + ' ' +
Convert(Varchar(10),ISNULL(User_Name(),'dbo')) + ' Created'
Print '
************************************************** *****************************/'
Print ' ---------------------------'
Print ' SET NOCOUNT ON'
Print ' SET ARITHABORT OFF'
Print ' SET QUOTED_IDENTIFIER OFF'
Print ' SET ANSI_WARNINGS OFF'
Print ' ---------------------------'

--------------------------------------------
-- Added: 14 Apr 2006
-- SECTION: Trim ALL Char/Varchar Input Type
--------------------------------------------
Print ' -----------------------------------'
Print ' --Trim ALL Char/Varchar Input Type '
Print ' -----------------------------------'
Select ' Select @' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ ' = ' + 'LTRIM(RTRIM(ISNULL('
+ ' @' + Replace(Replace(ColumnName,' ',''),'-','_')
+ Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ ' ,''''))) '
From #t
Where DataType IN
(
'char'
,'varchar'
,'nchar'
,'nvarchar'
)
Order By ColumnID
-------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
Column name(s))>

-------------------------------------------------------------------------

Print ' --------------------'
Print ' --Begin Transaction '
Print ' --------------------'
Print ' Begin Transaction'
Print ''

Print ' Insert INTO ' + @TableName
Print ' ('

Select ' ' + ' ' + '[' + ColumnName + ']'
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID = 2 -- First NON-Identity column
does not start with a PREFIX ","
--UNION
Select ' ' + ',' + '[' + ColumnName + ']'
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID 2
Order By ColumnID
---------------------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
respective Parameter name(s))>

---------------------------------------------------------------------------------------
Print ' )'
Print ' Select '

Select ' ' + ' @' + Replace(Replace(ColumnName,'
',''),'-','_')
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID = 2 -- First NON-Identity column
does not start with a PREFIX ","
--UNION
Select ' ' + ',@' + Replace(Replace(ColumnName,'
',''),'-','_')
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID 2
Order By ColumnID

Print ' IF @@Error != 0'
Print ' Begin'
Print ' ROLLBACK Transaction'
Print ' RaisError (''INSERT on Table ' +
@TableName + ' Failed!!!'',18,127)'
Print ' End'
Print ' Else'
Print ' Begin'
Print ' COMMIT Transaction'
Print ' End'

---------------------------------------
-- <SECTION: Object Creation -- Ending>
---------------------------------------
Print 'End'
Print 'go'
Print ''

Print 'IF @@Error = 0'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Insert
+ '] Created Sucessfully!!!'''
Print ' End'
Print 'Else'
Print ' Begin'
Print ' RaisError (''Stored Procedure [' +
@SPName_Insert + '] Creation Failed!!!'',18,127)'
Print ' End'
Print 'GO'
Print ''
End
----------------------------------
-- Start [Delete Stored Procedure]
----------------------------------
IF @SPType = 'D'
Begin
--------------------------------------------
-- <SECTION: Object Existence Check + DROP >
--------------------------------------------
Print 'If Exists ( Select * From SysObjects '
Print ' Where ID = Object_ID(N''[' +
@SPName_Delete + ']'') '
Print ' And OBJECTPROPERTY(id,
N''IsProcedure'') = 1 )'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Delete
+ '] Dropped Sucessfully!!!'''
Print ' Drop Procedure [' + @SPName_Delete + ']'
Print ' End'
Print 'GO'
Print ''
-----------------------------------------
-- <SECTION: Object Creation -- Starting>
-----------------------------------------
Print 'CREATE Procedure ' + @SPName_Delete
Select ' @' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))

+ ' ' + DataType
+ Case WHEN DataType = 'char' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'varchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nvarchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
Else ''
End
From #t
Where IdentityColumn = 1 -- Delete Should be performed
based on [Primary Key] Column

Print 'AS'
Print 'Begin'
Print '
/************************************************** ****************************'
Print ' * File : ' + @SPName_Delete
Print ' * PURPOSE : SP to DELETE Data from Table ' +
@TableName
Print ' * DATE AUTHOR DESCRIPTION'
Print '
*------------------------------------------------------------------------------'
Print ' * ' + Convert(Varchar(12),Getdate()) + ' ' +
Convert(Varchar(10),ISNULL(User_Name(),'dbo')) + ' Created'
Print '
************************************************** *****************************/'
Print ' ---------------------------'
Print ' SET NOCOUNT ON'
Print ' SET ARITHABORT OFF'
Print ' SET QUOTED_IDENTIFIER OFF'
Print ' SET ANSI_WARNINGS OFF'
Print ' ---------------------------'
-------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
Column name(s))>

-------------------------------------------------------------------------

Print ' --------------------'
Print ' --Begin Transaction '
Print ' --------------------'
Print ' Begin Transaction'
Print ''

Print ' Delete From ' + @TableName
Select ' Where ' + '[' + ColumnName + ']' + ' = ' + '@'
+ Replace(Replace(ColumnName,' ',''),'-','_')
From #t
Where IdentityColumn = 1
---------------------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
respective Parameter name(s))>

---------------------------------------------------------------------------------------

Print ' IF @@Error != 0'
Print ' Begin'
Print ' ROLLBACK Transaction'
Print ' RaisError (''DELETE from Table ' +
@TableName + ' Failed!!!'',18,127)'
Print ' End'
Print ' Else'
Print ' Begin'
Print ' COMMIT Transaction'
Print ' End'

---------------------------------------
-- <SECTION: Object Creation -- Ending>
---------------------------------------
Print 'End'
Print 'go'
Print ''

Print 'IF @@Error = 0'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Delete
+ '] Created Sucessfully!!!'''
Print ' End'
Print 'Else'
Print ' Begin'
Print ' RaisError (''Stored Procedure [' +
@SPName_Delete + '] Creation Failed!!!'',18,127)'
Print ' End'
Print 'GO'
Print ''
End

----------------------------------
-- Start [Update Stored Procedure]
----------------------------------
IF @SPType = 'U'
Begin
--------------------------------------------
-- <SECTION: Object Existence Check + DROP >
--------------------------------------------
Print 'If Exists ( Select * From SysObjects '
Print ' Where ID = Object_ID(N''[' +
@SPName_Update + ']'') '
Print ' And OBJECTPROPERTY(id,
N''IsProcedure'') = 1 )'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Update
+ '] Dropped Sucessfully!!!'''
Print ' Drop Procedure [' + @SPName_Update + ']'
Print ' End'
Print 'GO'
Print ''

-----------------------------------------
-- <SECTION: Object Creation -- Starting>
-----------------------------------------
Print 'CREATE Procedure ' + @SPName_Update

Select ' @' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))

+ ' ' + DataType
+ Case WHEN DataType = 'char' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'varchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nvarchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
Else ''
End
From #t
Where IdentityColumn != 0 -- Insert Should never account
for IdentityColumn Column

Select ' ,@' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))

+ ' ' + DataType
+ Case WHEN DataType = 'char' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'varchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
WHEN DataType = 'nvarchar' Then '(' +
Convert(Varchar,ColumnLength) + ')'
Else ''
End
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
Print 'AS'
Print 'Begin'
Print '
/************************************************** ****************************'
Print ' * File : ' + @SPName_Update
Print ' * PURPOSE : SP to UPDATE Data in Table ' +
@TableName
Print ' * DATE AUTHOR DESCRIPTION'
Print '
*------------------------------------------------------------------------------'
Print ' * ' + Convert(Varchar(12),Getdate()) + ' ' +
Convert(Varchar(10),ISNULL(User_Name(),'dbo')) + ' Created'
Print '
************************************************** *****************************/'
Print ' ---------------------------'
Print ' SET NOCOUNT ON'
Print ' SET ARITHABORT OFF'
Print ' SET QUOTED_IDENTIFIER OFF'
Print ' SET ANSI_WARNINGS OFF'
Print ' ---------------------------'

--------------------------------------------
-- Added: 14 Apr 2006
-- SECTION: Trim ALL Char/Varchar Input Type
--------------------------------------------
Print ' -----------------------------------'
Print ' --Trim ALL Char/Varchar Input Type '
Print ' -----------------------------------'
Select ' Select @' + Replace(Replace(ColumnName,'
',''),'-','_') + Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ ' = ' + 'LTRIM(RTRIM('
+ ' @' + Replace(Replace(ColumnName,' ',''),'-','_')
+ Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ ' )) '
From #t
Where DataType IN
(
'char'
,'varchar'
,'nchar'
,'nvarchar'
)
Order By ColumnID
-------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
Column name(s))>

-------------------------------------------------------------------------

Print ' --------------------'
Print ' --Begin Transaction '
Print ' --------------------'
Print ' Begin Transaction'
Print ''

Print ' Update ' + @TableName
Print ' SET '

Select ' ' + '[' + ColumnName + ']' + ' = '
+
Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ '@' +
Replace(Replace(ColumnName,' ',''),'-','_')
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID = 2 -- First NON-Identity column
does not start with a PREFIX ","
--UNION
Select ' ,' + '[' + ColumnName + ']' + ' = '
+
Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ '@' +
Replace(Replace(ColumnName,' ',''),'-','_')
From #t
Where IdentityColumn = 0 -- Insert Should never account
for IdentityColumn Column
And ColumnID 2 -- First NON-Identity column
does not start with a PREFIX ","
Order By ColumnID

Select ' Where ' + '[' + ColumnName + ']' + ' = '
+
Space(@ColumnNameMAXLength-(DataLength(ColumnName)/2))
+ '@' +
Replace(Replace(ColumnName,' ',''),'-','_')
From #t
Where IdentityColumn = 1
---------------------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
respective Parameter name(s))>

---------------------------------------------------------------------------------------

Print ' IF @@Error != 0'
Print ' Begin'
Print ' ROLLBACK Transaction'
Print ' RaisError (''UPDATE On Table ' +
@TableName + ' Failed!!!'',18,127)'
Print ' End'
Print ' Else'
Print ' Begin'
Print ' COMMIT Transaction'
Print ' End'

---------------------------------------
-- <SECTION: Object Creation -- Ending>
---------------------------------------
Print 'End'
Print 'go'
Print ''

Print 'IF @@Error = 0'
Print ' Begin'
Print ' Print ''Stored Procedure [' + @SPName_Update
+ '] Created Sucessfully!!!'''
Print ' End'
Print 'Else'
Print ' Begin'
Print ' RaisError (''Stored Procedure [' +
@SPName_Update + '] Creation Failed!!!'',18,127)'
Print ' End'
Print 'GO'
Print ''
End
-------------------------------------
-- Start [SelectAll Stored Procedure]
-------------------------------------
IF @SPType = 'SA'
Begin
--------------------------------------------
-- <SECTION: Object Existence Check + DROP >
--------------------------------------------
Print 'If Exists ( Select * From SysObjects '
Print ' Where ID = Object_ID(N''[' +
@SPName_SelectAll + ']'') '
Print ' And OBJECTPROPERTY(id,
N''IsProcedure'') = 1 )'
Print ' Begin'
Print ' Print ''Stored Procedure [' +
@SPName_SelectAll + '] Dropped Sucessfully!!!'''
Print ' Drop Procedure [' + @SPName_SelectAll + ']'
Print ' End'
Print 'GO'
Print ''

-----------------------------------------
-- <SECTION: Object Creation -- Starting>
-----------------------------------------
Print 'CREATE Procedure ' + @SPName_SelectAll
Print 'AS'
Print 'Begin'
Print '
/************************************************** ****************************'
Print ' * File : ' + @SPName_SelectAll
Print ' * PURPOSE : SP to Select All Data from Table ' +
@TableName
Print ' * DATE AUTHOR DESCRIPTION'
Print '
*------------------------------------------------------------------------------'
Print ' * ' + Convert(Varchar(12),Getdate()) + ' ' +
Convert(Varchar(10),ISNULL(User_Name(),'dbo')) + ' Created'
Print '
************************************************** *****************************/'
Print ' ---------------------------'
Print ' SET NOCOUNT ON'
Print ' SET ARITHABORT OFF'
Print ' SET QUOTED_IDENTIFIER OFF'
Print ' SET ANSI_WARNINGS OFF'
Print ' ---------------------------'
-------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
Column name(s))>

-------------------------------------------------------------------------
Print ' ' + 'Select '
Select ' ' + ' ' + '[' + ColumnName + ']'
From #t
Where ColumnID = 1
--UNION
Select ' ' + ',' + '[' + ColumnName + ']'
From #t
Where ColumnID 1
Order By ColumnID
Print ' ' + 'From ' + @TableName
Print ' ' + 'Where Status = 1'

---------------------------------------
-- <SECTION: Object Creation -- Ending>
---------------------------------------
Print 'End'
Print 'go'
Print ''

Print 'IF @@Error = 0'
Print ' Begin'
Print ' Print ''Stored Procedure [' +
@SPName_SelectAll + '] Created Sucessfully!!!'''
Print ' End'
Print 'Else'
Print ' Begin'
Print ' RaisError (''Stored Procedure [' +
@SPName_SelectAll + '] Creation Failed!!!'',18,127)'
Print ' End'
Print 'GO'
Print ''
End

-----------------------------------------
-- Start [SelectUsingPK Stored Procedure]
-----------------------------------------
IF @SPType = 'SPK'
Begin
--------------------------------------------
-- <SECTION: Object Existence Check + DROP >
--------------------------------------------
Print 'If Exists ( Select * From SysObjects '
Print ' Where ID = Object_ID(N''[' +
@SPName_SelectUsingPK + ']'') '
Print ' And OBJECTPROPERTY(id,
N''IsProcedure'') = 1 )'
Print ' Begin'
Print ' Print ''Stored Procedure [' +
@SPName_SelectUsingPK + '] Dropped Sucessfully!!!'''
Print ' Drop Procedure [' + @SPName_SelectUsingPK +
']'
Print ' End'
Print 'GO'
Print ''

-----------------------------------------
-- <SECTION: Object Creation -- Starting>
-----------------------------------------
Print 'CREATE Procedure ' + @SPName_SelectUsingPK
Print 'AS'
Print 'Begin'
Print '
/************************************************** ****************************'
Print ' * File : ' + @SPName_SelectUsingPK
Print ' * PURPOSE : SP to Select All Data from Table ' +
@TableName
Print ' * DATE AUTHOR DESCRIPTION'
Print '
*------------------------------------------------------------------------------'
Print ' * ' + Convert(Varchar(12),Getdate()) + ' ' +
Convert(Varchar(10),ISNULL(User_Name(),'dbo')) + ' Created'
Print '
************************************************** *****************************/'
Print ' ---------------------------'
Print ' SET NOCOUNT ON'
Print ' SET ARITHABORT OFF'
Print ' SET QUOTED_IDENTIFIER OFF'
Print ' SET ANSI_WARNINGS OFF'
Print ' ---------------------------'
-------------------------------------------------------------------------
-- <SECTION: Object Creation -- Body of the SP (Identify
Column name(s))>

-------------------------------------------------------------------------
Print ' ' + 'Select '
Select ' ' + ' ' + '[' + ColumnName + ']'
From #t
Where ColumnID = 1
--UNION
Select ' ' + ',' + '[' + ColumnName + ']'
From #t
Where ColumnID 1
Order By ColumnID
Print ' ' + 'From ' + @TableName
Print ' ' + 'Where Status = 1'
Print ' ' + 'AND ' + @TableName + 'ID' + ' = ' +
'@' + @TableName + 'ID'
---------------------------------------
-- <SECTION: Object Creation -- Ending>
---------------------------------------
Print 'End'
Print 'go'
Print ''

Print 'IF @@Error = 0'
Print ' Begin'
Print ' Print ''Stored Procedure [' +
@SPName_SelectUsingPK + '] Created Sucessfully!!!'''
Print ' End'
Print 'Else'
Print ' Begin'
Print ' RaisError (''Stored Procedure [' +
@SPName_SelectUsingPK + '] Creation Failed!!!'',18,127)'
Print ' End'
Print 'GO'
Print ''
End
End
go

IF @@Error = 0
Begin
Print 'Stored Procedure [GenerateStoredProcedureTemplate]
Created Sucessfully!!!'
End
Else
Begin
RaisError ('Stored Procedure [GenerateStoredProcedureTemplate]
Creation Failed!!!',18,127)
End
GO

--Exec GenerateStoredProcedureTemplate @TableName = 'Challenges',
@SPType = 'D'
--go

/*
Select 'Exec GenerateStoredProcedureTemplate @TableName = ''' + Name +
''', @SPType = ''D''' + Char(10) + 'Go'
>From SysObjects
Where Type = 'u'
Order By Name
*/

Sep 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: paul | last post by:
Hi, I'd like to generate PDF files from my Php website. I've found PDFLib to make it... but i'd like to have your views. It is the best I can find ? Are there any other PDF generating lib ?...
0
by: Dan Gass | last post by:
The difflib.py module and the diff.py tools script in Python 2.4 alpha 3 now support generating side by side (with intra line differences) in HTML format. I have found this useful for performing...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
17
by: flupke | last post by:
Hi, i create my GUIs mainly via wxGlade. However when you start of to program and want to do some rearranging to the gui, wxglade overwrites your file and you've got to put your own code back...
3
by: skn | last post by:
Hello., Does the python compiler provide an option to generate a .pyo(optimized byte code file) from a .py (source file)? For generating .pyc I know that I only have to pass the source file...
3
by: Raed Sawalha | last post by:
Hello when I serialize an object an error generated using this function public string SerializeObject(object oClassObject,System.Type oClassType) { XmlSerializer oSerializer = new...
1
by: Nathan Sokalski | last post by:
Visual Studio 2005 recently stopped generating the *.designer.vb files for my *.aspx and *.ascx files. I am using Service Pack 1, and do not believe I did anything differently than normal prior to...
3
by: deciacco | last post by:
I'm trying to write a label printing SDI app with a small preview on the main form itself. Every time I run the InvalidatePreview event on the preview control to redraw the preview I get the...
0
by: Aswanth | last post by:
I'm Generating Reports in SSRS-2005.. Previously I got the Data from One Database & Generated Reports.. Now I used to get the Data from Two Different Databases(ie Database-1 & Database-2) & to...
0
by: Aswanth | last post by:
I'm Working with Asp.Net with C#.. & I'm Generating Reports in SSRS-2005.. Till Now I'm Generating Reports in SSRS-2005 with Stored Procedure.. in Which I'm Generating Reports for One...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.