Named Instances SQL Server

misermice
misermice used Ask the Experts™
on
I have two named instances in my SSMS using SS08
If i connect to both instances
Both Instances have same DB (Cars)
If i open a Querry window and create a table with following syntax(attached Code Snippet)

A Table was created in Instance SYED but not in DEVDB ?

Why so ?

How do i direct the operations/Commands to a particular Instance ?

I have two instances SYED and DEVDB (attached Image)


USE Cars
CREATE TABLE Rental_Cars
     ( 
        CarID               INT                NOT NULL  , 
        brandname         VARCHAR(20)          NULL  , 
        modelname          VARCHAR(30)         NULL  , 
        manuf_year        INT				   NULL,
        CONSTRAINT PK_Rental_Cars PRIMARY KEY CLUSTERED (CarID ASC) 
        ON [PRIMARY] 
     )
GO

Open in new window

Capture.PNG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

CREATE TABLE Syed.Cars..Rental_Cars
     (
        CarID               INT                NOT NULL  ,
        brandname         VARCHAR(20)          NULL  ,
        modelname          VARCHAR(30)         NULL  ,
        manuf_year        INT                           NULL,
        CONSTRAINT PK_Rental_Cars PRIMARY KEY CLUSTERED (CarID ASC)
        ON [PRIMARY]
     )
GO



CREATE TABLE DEVDB.Cars..Rental_Cars
     (
        CarID                INT                NOT NULL  ,
        brandname          VARCHAR(20)          NULL  ,
        modelname          VARCHAR(30)          NULL  ,
        manuf_year        INT                           NULL,
         CONSTRAINT PK_Rental_Cars PRIMARY KEY CLUSTERED (CarID ASC)
         ON [PRIMARY]
     )
GO

observe, there was a two dot before table name, if you want to use specific schema name, you can put it between two dot, otherwise it will use default schema, may be "DBA".
SQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018
Commented:
Connect to Syed instance in SSMS and then execute the create table statement in that instance on Cars database.
Similarly connect to Devdb instance in SSMS and then execute the create table statement in that instance on Cars database.

You can't create table in both instances from a single connection.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

you can right click on database and click on "New Query" option on each database and create table. Above answer is right, you can't make table in both DB from one connection. However, you can access table from the four part naming convention (if you have permission though) as  I told you above but can't create table.

Author

Commented:
So is this the FOUR PART NAMING CONVENTION?

SELECT * FROM SERVER.DATABASE.OWNER/SCHEMA.OBJECT

IF SO..

Is Instance synonymous to OWNER/SCHEMA ? (SYED or DEVDB in my case) ?
no, server is synonymous  to SYED or DEVDB. actually there should be full server name, especially if it is a named instance.
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018
Commented:
>> Is Instance synonymous to OWNER/SCHEMA

No, this is how instances and other objects are organized.

Single server (machine)
-> n Instances
--> Each instances can have n no. of databases
---> Instances can have schema and tables inside it

>> SERVER.DATABASE.OWNER/SCHEMA.OBJECT

Four part naming refers to servername.dbname.schemaname.tablename

And you can use four part naming if you have created Linked server to your other instance.
Without which you can't create tables in your other instance.

Author

Commented:
But When i try to access

a particular DB say Cars

like
USE SYED.CARS
SELECT * FROM sys.Tables

or

USE DEVDB.CARS
SELECT * FROM sys.Tables

It says

Database SYED doest not exist and  Database DEVDB doest not exist respectively
Why is not identifying Instance specific commands ?

Am i accessing it in wrong way ?
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018

Commented:
You need to get clarified between Instances and Databases in it and the way you need to create tables inside Cars database in both of those instances

Connect to your Syed instance in SSMS, Right click it and then choose a New Query and then try

USE cars
-- CREATE table statement here
-- Issue select * from sys.tables to get it created

Similarly connect to your DevDB instance and follow the steps mentioned above.
Else you will get the errors mentioned above..
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
Just rephrasing what other experts said:

>>How do i direct the operations/Commands to a particular Instance ?
Right-click on the database in the right instance on the tree on the left - "New Query" - this will be the instance your SQL will run against.
You must do this if you want to create table or perform DDL operations.

>>So is this the FOUR PART NAMING CONVENTION?
select * from [ZAFAR-PC\DEVDB].Cars.sys.objects
select * from [.\SYED].Cars.sys.objects

But for the 4-part naming to work, you need to link the servers first.  For example, run this from ZAFAR...
Replace rmtuser and rmtpassword with the login to connect to .\syed

sp_addlinkedserver [.\SYED]
sp_addlinkedsrvlogin [.\SYED], @rmtuser = 'rmtuser', @rmtpassword= 'rmtpassword'

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial