Solved

stored procedure problem

Posted on 2004-07-30
4
384 Views
Last Modified: 2006-11-17
I am trying to write a stored procedure that I can execute with a given client name, and pass that client name as param to the create table statement inside the stored procedure.  Here is the syntax I have:

IF EXISTS (SELECT name
         FROM   sysobjects
         WHERE  name = 'Build_Invoice'
         AND         type = 'P')
    DROP PROCEDURE Build_Invoice
GO

CREATE PROCEDURE Build_Invoice (@Client Varchar(30) = " ")
AS
Set @Client=@Client + '_Invoice'

CREATE TABLE dbo.@Client (
      [Column1] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
                [Column2] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO


I am testing in by running this:

execute Build_Invoice 'Test' and expecting to get Test_Invoice as table name.  I can create the sp just fine, but I cannot pass the param.  Help!!!

Thanks in advance,

IPT
0
Comment
Question by:iptrader
  • 2
4 Comments
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11681414
CREATE PROCEDURE Build_Invoice (@Client Varchar(30) = " ")
AS
begin

  set nocount on

  declare @sql varchar(8000)

  set @sql = 'CREATE TABLE dbo.[' + @client + '_invoice'] (' +
      '[Column1] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , ' +
      '[Column2] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL  ' +
      ') ON [PRIMARY] '

  execute (@sql)

  set nocount off
end
go

But why not create one invoice table, with ClientName as a field? It would simplify SO many things...
0
 

Author Comment

by:iptrader
ID: 11681508
The table is actually part of an overall bigger beast involving an ETL process.  Metadata for this table HAS to be exact.

:)
0
 
LVL 18

Accepted Solution

by:
SjoerdVerweij earned 350 total points
ID: 11681993
Alrighty. Did my code work though?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11683354
Then would it not be better to have a Standard set of tables....

and use the schema name to separate the clients ...

ie create     CommonDB.Client1.invoice  

also you sp should be more like...
CREATE PROCEDURE Build_Invoice (@Client Varchar(30) = " ")
AS
Declare @TableName Varchar(40)
declare @sql varchar(8000)

Set @TableNAme=@Client + '_Invoice'

set @SQL = 'CREATE TABLE dbo.' + @TableName
 +' ( [Column1] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,'
+'   [Column2] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL '
+ ') ON [PRIMARY]'

exec (@sql)
set @SQL = 'GRANT Select on dbo.' + @tablename + ' to ' + @client
exec (@SQL)

GO
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Search query matching words 20 38
push and Pull replication 31 46
Database maintenance 36 98
SQL Server maintenance plan not deleting backup files as anticipated 4 14
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question