April 19, 2017 at 12:47 pm
I have an EXEC @sql script which creates a view.
Reason for the EXEC @sql script is that the library and schema are passed as parameters to the procedure.
The CREATE VIEW needs to be the first statement in the script
and I can't designate the library.
Is there a way to set the library within the script?
Chas
April 19, 2017 at 12:51 pm
What do you mean by 'library'?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 20, 2017 at 5:13 am
If you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?
John
April 20, 2017 at 5:45 am
John Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
I have a procedures set up which allows users to enter specific table names from different databases/schemas.
If the views are not there, the procedure will create them.
April 20, 2017 at 5:46 am
John Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
John Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
John Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
chas.1 - Wednesday, April 19, 2017 12:47 PMI have an EXEC @sql script which creates a view.
Reason for the EXEC @sql script is that the library and schema are passed as parameters to the procedure.
The CREATE VIEW needs to be the first statement in the script
and I can't designate the library.
Is there a way to set the library within the script?
Chas
GilaMonster - Wednesday, April 19, 2017 12:51 PMWhat do you mean by 'library'?
Sorry, I meant database.
April 20, 2017 at 5:59 am
chas.1 - Thursday, April 20, 2017 5:45 AMJohn Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
I have a procedures set up which allows users to enter specific table names from different databases/schemas.
If the views are not there, the procedure will create them.
Again, if you're relying on your users' understanding of the tables in the database, probably bad design. You'll be left with hundreds or thousands of views in your database that are only used once. How does the procedure know how to create the views? Are the views simply a UNION of the tables specified by the user? If that's the case, you almost certainly need to normalise your database so that you don't have the same table repeated in different databases and schemas.
Going back to your original question, though, if you want to set the library (database) within the script, simply use a three-part naming scheme: MyDatabase.MySchema.MyTable.
John
April 20, 2017 at 6:53 am
John Mitchell-245523 - Thursday, April 20, 2017 5:59 AMchas.1 - Thursday, April 20, 2017 5:45 AMJohn Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
I have a procedures set up which allows users to enter specific table names from different databases/schemas.
If the views are not there, the procedure will create them.Again, if you're relying on your users' understanding of the tables in the database, probably bad design. You'll be left with hundreds or thousands of views in your database that are only used once. How does the procedure know how to create the views? Are the views simply a UNION of the tables specified by the user? If that's the case, you almost certainly need to normalise your database so that you don't have the same table repeated in different databases and schemas.
Going back to your original question, though, if you want to set the library (database) within the script, simply use a three-part naming scheme: MyDatabase.MySchema.MyTable.
John
But CREATE VIEW only allows schema and table. I need to set the database, but the CREATE VIEW statement needs to be first in the batch.
Chas
April 20, 2017 at 7:08 am
Chas
So from one database, you want to create a view in another database? That's not possible, as far as I know. Even if it were, imagine all the permissions you'd need in each other's databases - it would be a security nightmare. Is this something new you're designing, or are you attempting to bolt something on to an already bad design?
John
April 20, 2017 at 7:14 am
chas.1 - Thursday, April 20, 2017 5:45 AMJohn Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
I have a procedures set up which allows users to enter specific table names from different databases/schemas.
If the views are not there, the procedure will create them.
This is an extremely unusual requirement, but I have seen it before - something to do with mailing lists for conferences. What exactly does this application do, out of interest?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 20, 2017 at 11:29 am
ChrisM@Work - Thursday, April 20, 2017 7:14 AMchas.1 - Thursday, April 20, 2017 5:45 AMJohn Mitchell-245523 - Thursday, April 20, 2017 5:13 AMIf you're having to create views dynamically at run time, that suggests to me that the database design may be wrong. Please will you share a bit more information on what your application does and why you don't create the views at design time?John
I have a procedures set up which allows users to enter specific table names from different databases/schemas.
If the views are not there, the procedure will create them.This is an extremely unusual requirement, but I have seen it before - something to do with mailing lists for conferences. What exactly does this application do, out of interest?
John,
I work in a place with many different databases. I have been creating several procedures using dynamic SQL and substituting database and schema.
That way many users can use the same procedure(s).
Chas
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply