Script to create a Table from Stored Procedure parameters

So, you're thinking 'what??'

Why would you want to create a table from the parameters of a stored procedure?

Well, I've got a good reason. I've struggled for years with a way to pass parameters into eConnect code, and to have a piece of eConnect code that is portable between clients. That's really important for me, I work at a different client every day. So, code portability makes me money.

The idea is that I take the eConnect stored procedures and create tables that mirror the procs. The sending code populates the table and then calls the eConnect library which knows to pick up the data from the tables and run.

I still have to edit some of it... but a majority of the work is done. And there is no need to reference any eConnect objects in any code except the eConnect library.

This code loops through the parameters of a stored procedure and creates a table.

 

IF EXISTS (
  SELECT *
    FROM INFORMATION_SCHEMA.ROUTINES
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'dd_CreateTableFromStoredProcedureParameters'
)
   DROP PROCEDURE dbo.dd_CreateTableFromStoredProcedureParameters
GO
 
CREATE PROCEDURE dbo.dd_CreateTableFromStoredProcedureParameters
--  dd_CreateTableFromStoredProcedureParameters 'taRMCashReceiptInsert'
 
@StoredProcedureName varchar(100)
 
AS
 
set nocount on
DECLARE @text varchar(100)
print 'CREATE TABLE DD' + @StoredProcedureName
print '('
PRINT 'RowID int NOT NULL IDENTITY (1, 1),'
 
   
DECLARE curName CURSOR LOCAL FAST_FORWARD FOR
    select rtrim(replace(PARAMETER_NAME,'@I_v','')) + ' ' + DATA_TYPE +
            case DATA_TYPE
                when 'char'  then ' (' + convert(varchar(20),CHARACTER_MAXIMUM_LENGTH) + ') '
                when 'varchar'  then ' (' + convert(varchar(20),CHARACTER_MAXIMUM_LENGTH) + ') '
                when 'datetime' then ' '
                when 'date' then ' '
                when 'numeric' then ' (' + convert(varchar(20),NUMERIC_PRECISION) + ',' + convert(varchar(20),NUMERIC_SCALE) + ') '
                when 'smallint' then ' '
                when 'int' then ' '
                when 'bigint' then ' '
                when 'bit' then ' '
                else 'zzz ' end +
            'NOT NULL' +
            case when ROW_NUMBER() over (partition by specific_name order by ordinal_position) = MAX(ORDINAL_POSITION) over (partition by specific_name) then '' else ',' end
        from INFORMATION_SCHEMA.PARAMETERS
        where SPECIFIC_NAME = 'taRMCashReceiptInsert'
            and not PARAMETER_NAME in ('@O_iErrorState','@oErrString')
   
OPEN curName
WHILE 1=1
BEGIN
    FETCH NEXT FROM curName INTO @text
    if @@fetch_status <> 0 begin
        break
    end
   
    print @text 
END
CLOSE curName
DEALLOCATE curName
 
print ')  ON [PRIMARY]'
 
print 'GO'
Print 'ALTER TABLE DD' + @StoredProcedureName + ' ADD CONSTRAINT'
Print ' PK_DDErrorLog PRIMARY KEY CLUSTERED '
Print ' ('
Print ' RowID'
Print ' ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]'
 
Print 'GO'
 
 
 
grant exec on dd_CreateTableFromStoredProcedureParameters to public
 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences