SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


''CREATE VIEW'' does not allow specifying the database name as a prefix to the object name


''CREATE VIEW'' does not allow specifying the database name as a prefix to the object name

Author
Message
BEN-405167
BEN-405167
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 40

For each customer a separate database is created. The variable @dbname is set from within a .dotnet application

ALTER PROC SP_CREATE_DATABASE_BASIS
@dbname sysname
AS

DECLARE @cmd varchar(4000)

SET @cmd = 'CREATE TABLE ' + @dbname + '.dbo.ETL_LAAD (
LAAD_KEY bigint IDENTITY (1, 1) NOT NULL ,
LAAD_DATUM datetime NOT NULL
) ON [PRIMARY]'
--EXEC @cmd

Not only tables have to be created but 1 view as well.

But 'CREATE VIEW' does not allow specifying the database name.

Is there a workaround?


BEN-405167
BEN-405167
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 40

You can, however, use the three part name for the table(s) used in the VIEW definition.


Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40132 Visits: 38567

Try this:

SET @cmd = 'USE ' + @dbname + ';CREATE TABLE dbo.ETL_LAAD (LAAD_KEY bigint IDENTITY (1, 1) NOT NULL , LAAD_DATUM datetime NOT NULL) ON [PRIMARY]'
--EXEC @cmd



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
BEN-405167
BEN-405167
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 40

I tried this but i get :

The name 'USE db_presmon_dwh_9;CREATE TABLE dbo.ETL_TEST (LAAD_KEY bigint IDENTITY (1, 1) NOT NULL , LAAD_DATUM datetime NOT NULL) ON [PRIMARY]' is not a valid identifier.


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87948 Visits: 41128

Must be...

EXEC (@Cmd)



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
BEN-405167
BEN-405167
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 40

I tried this:

ALTER procedure SP_CREATE_VIEW_TEST

@dbname sysname,
@Klant_ID bigint

as

DECLARE @cmd varchar(3000)

SET @cmd = 'USE ' + @dbname + ';CREATE VIEW FACTUURGEGEVENS_' + convert(varchar,@Klant_ID) + ' as
SELECT ikp.IKP_GROEPNAAM,
ikp.IKP_NAAM,
ks.KS_NUMMER,
ks.KS_NAAM,
lev.LEV_CREDITEURNUMMER,
lev.LEV_NAAM,
lev.LEV_GROEPNAAM,
org.ORG_KOSTENPLAATS_NUMMER,
org.ORG_KOSTENPLAATS_NAAM,
org.ORG_AFDELING_NAAM,
adm.ADM_CODE,
ft.FEIT_BOEKSTUKNUMMER,
ft.FEIT_FACTUUROMSCHRIJVING,
ft.FEIT_FIN_JAAR,
ft.FEIT_FACTUUR_REGEL_BEDRAG,
ft.FEIT_FACTUUR_NUMMER

FROM '+ @dbname +'.dbo.DIM_ADMINISTRATIE adm INNER JOIN
'+ @dbname +'.dbo.FEIT_BOEKREGEL ft ON adm.ADM_KEY = ft.ADM_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_INKOOPPAKKET ikp ON ft.IKP_KEY = ikp.IKP_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_LEVERANCIER lev ON ft.LEV_KEY = lev.LEV_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_ORGANISATIE org ON ft.ORG_KEY = org.ORG_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_KOSTENSOORT ks ON ft.KS_KEY = ks.KS_KEY'
EXEC (@cmd)


I get the message:

'CREATE VIEW' must be the first statement in a query batch.


goodguy
goodguy
SSC-Addicted
SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)

Group: General Forum Members
Points: 444 Visits: 1037
I normally place a GO before any CREATE statements to distinguish the BATCH.

SET @cmd = 'USE ' + @dbname + ';GO;CREATE VIEW...


Regards,

goodguy

Experience is a bad teacher whose exams precede its lessons

BEN-405167
BEN-405167
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 40

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER procedure SP_CREATE_VIEW_TEST


@dbname sysname,
@Klant_ID bigint

as

DECLARE @cmd varchar(3000)

SET @cmd = 'USE ' + @dbname + ';GO;CREATE VIEW FACTUURGEGEVENS_' + convert(varchar,@Klant_ID) + ' as
SELECT ikp.IKP_GROEPNAAM,
ikp.IKP_NAAM,
ks.KS_NUMMER,
ks.KS_NAAM,
lev.LEV_CREDITEURNUMMER,
lev.LEV_NAAM,
lev.LEV_GROEPNAAM,
org.ORG_KOSTENPLAATS_NUMMER,
org.ORG_KOSTENPLAATS_NAAM,
org.ORG_AFDELING_NAAM,
adm.ADM_CODE,
ft.FEIT_BOEKSTUKNUMMER,
ft.FEIT_FACTUUROMSCHRIJVING,
ft.FEIT_FIN_JAAR,
ft.FEIT_FACTUUR_REGEL_BEDRAG,
ft.FEIT_FACTUUR_NUMMER

FROM '+ @dbname +'.dbo.DIM_ADMINISTRATIE adm INNER JOIN
'+ @dbname +'.dbo.FEIT_BOEKREGEL ft ON adm.ADM_KEY = ft.ADM_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_INKOOPPAKKET ikp ON ft.IKP_KEY = ikp.IKP_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_LEVERANCIER lev ON ft.LEV_KEY = lev.LEV_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_ORGANISATIE org ON ft.ORG_KEY = org.ORG_KEY INNER JOIN
'+ @dbname +'.dbo.DIM_KOSTENSOORT ks ON ft.KS_KEY = ks.KS_KEY'
EXEC (@cmd)


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

I still get:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'GO'.
Server: Msg 111, Level 15, State 1, Line 1
'CREATE VIEW' must be the first statement in a query batch.
Stored Procedure: DB_PRESMON_REPOS.dbo.SP_CREATE_VIEW_TEST
Return Code = 0


steve smith-401573
steve smith-401573
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1033 Visits: 744
Is there a reason NOT to use two separate variables and place the view create in its own variable? Or do you end up then having the view placed in the wrong database? Which brings us back to using a fully qualified view name, including database, owner, and view name?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search