Solved

stored procedure problem

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: Episode III - Revenge of The Dude 24 52
Help Required 3 93
SQL Error - Query 6 24
SQL view 2 26
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

776 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