Solved

stored procedure problem

Posted on 2004-07-30
4
379 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now