August 19, 2018 at 12:09 am
SQL Server 2014 Enterprise Edition 64 bit
Windows Server 2012 R2 Enterprise Edition 64 bit
There are two databases A and B. I want to create views in Database B Using Tables in Database A. I am able to generate the code for it. But I am manually execute the code in Database B. I am looking for the code itself to execute dynamically and create the views in database B. I want to use no lock on all of the tables(Database A) in the views in Database B. I want one view with no lock on each table. Can you please help.
August 19, 2018 at 1:03 am
sql2k8 - Sunday, August 19, 2018 12:09 AMSQL Server 2014 Enterprise Edition 64 bit
Windows Server 2012 R2 Enterprise Edition 64 bitThere are two databases A and B. I want to create views in Database B Using Tables in Database A. I am able to generate the code for it. But I am manually execute the code in Database B. I am looking for the code itself to execute dynamically and create the views in database B. I want to use no lock on all of the tables(Database A) in the views in Database B. I want one view with no lock on each table. Can you please help.
You will have to elaborate further on this, provide DDL, what you have tried etc. Database A-Z doesn't mean anything in this context!
😎
You have been around long enough to know that no one can answer this kind of question without further information.
August 20, 2018 at 4:40 pm
DECLARE @SourceDB SYSNAME ;
DECLARE @TrgtDB SYSNAME
SET @SourceDB = 'DatabaseA'
SET @TrgtDB = 'DatabaseB'
DECLARE @CreateViewStatement NVARCHAR(MAX)
SET @CreateViewStatement = '
USE '+ QUOTENAME(@TrgtDB) +';
EXEC(''
CREATE VIEW [dbo].[vw_TestTable] AS
SELECT *
FROM [dbo].[TestTable] WITH (NOLOCK)
'')
'
EXEC (@CreateViewStatement)
This is creating views but taking table in database b and create view in database b. Just one table itself. I want all of the tables in database A to be used in creating views in database B.
August 20, 2018 at 4:42 pm
create table dbo.#QueryResults
(
QResults VARCHAR(MAX)
)
INSERT INTO dbo.#QueryResults(QResults)
select
'CREATE VIEW ' + 'vw_' + t.name + CHAR(13) + 'AS ' + CHAR(13) + 'SELECT * ' + CHAR(13) + 'FROM DatabaseA.' + s.name + '.[' + t.name +'] WITH (NOLOCK) ; ' + CHAR(13)
FROM sys.tables t
JOIN sys.schemas s
ON s.schema_id = t.schema_id
ORDER BY s.name, t.name ;
DECLARE @InsertString NVARCHAR(MAX) ;
DECLARE @QueryResults1 NVARCHAR(MAX) ;
set @QueryResults1 = 'SELECT * FROM dbo.#QueryResults'
set @InsertString = @QueryResults1
EXEC [DatabaseB].dbo.sp_executesql @InsertString ;
DROP TABLE dbo.#QueryResults ;
This one is creating the TSQL . I have to run it manually in database each view. I Cannot run the whole statement as a batch. If I do I am getting the below mentioned error message.
August 20, 2018 at 4:42 pm
Msg 156, Level 15, State 1, Procedure vw_TestC, Line 2
Incorrect syntax near the keyword 'CREATE'.
August 20, 2018 at 5:00 pm
Please defend your use of the NOLOCK hint. You do realize that this can be bad.
August 20, 2018 at 5:05 pm
USE JicViews
GO
DECLARE
@sql nvarchar(4000) = N'CREATE VIEW View_name AS SELECT * FROM Schema_table;'
, @create_view nvarchar(4000)
, @schema_table nvarchar(200) ;
DECLARE t CURSOR FORAugust 20, 2018 at 7:49 pm
Joe,
Thank You very much for the Help. It worked like a charm.
August 22, 2018 at 6:44 am
Why not create synonyms?
Synonyms are awesome because you can create them to have the same name in your target database as they are called in the source database and you don't have to edit the code as it moves through environments
Eg. you have been working on modifying a view which is now ready for production testing/release
In DEV you have a table TableX which actually lives in your database. views will use the native TableX
in UAT, TableX does not exist as a table object, instead it is a synonym that points to TableX in PROD. This way the UAT environment is using the live data, but running the logic from DEV
in PROD, you replace the existing view with the revised view and it now uses the native table in PROD.
Dev-Ops team are happy because the view that you deployed to live is EXACTLY THE SAME* as the view you checked in to source code control.
* not quite the same if you have WITH SCHEMABINDING as SCHEMABINDING can't work across databases.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply