Skip to main content

Accessing a table in SQL Server 2005 (Schema related)

Recently in a discussion forum I came across this question "I have a small Doubt in SQL server. The database name is 'AdventureWorks' and the table name is 'Production.Culture'. What is the query to fetch records from that table."

The answer to this is :

Select cultureid, [name], modifiedDate from AdventureWorks.Production.Culture

There was another member of the forum who was saying that we need to only use the below query:

Select * from AdventureWorks.dbo.Production.Culture

Please note that he has mentioned two Schema names (dbo and Production) in a single query. This query wouldn't work and I thought I would explain the concept of "Schemas" in SQL Server 2005 to him.

In short,

a) Schema is similar to Namespaces in .NET
b) It helps in logical grouping of tables.
c) To view all existing schema in a database, for example within AdventureWorksDB ::

1. Open ur SQL Mgmt studio
2. Go to AdventureWorks DB and expand it
3. Expand the Tables

4. Now just observe what you see within that. There would be,

i) few tables which starts with "dbo.",
ii) few tables which starts with "Person."
iii) few tables which starts with "Production." etc.,

Those are all called as "Schema Names".

Other easier way to find this is to, navigate to the "Security" tab of the Database. There we will find a folder by name "Schema". Expand that to see all the Schema names related to that DB.

5. Now click on "New query" button and try this

--- This would work
Select * from person.address

6. Now try this

--- This wouldn't work
Select * from dbo.person.address

7. Just for your confirmation, navigate to someother database in ur query window. For example,
Use Master
Go

--- This would work
Select * from adventureworks.person.address

--- This would work
Select * from adventureworks.dbo.AWBuildVersion

--- This would FAIL
Select * from adventureworks.dbo.person.address

8) That said, by DEFAULT when we create any new table it would be assigned to "dbo." schema.

This you can verify by going to ur SQL Mgmt Studio again, navigate to any database of your choice and create a sample table. Now expand the "Tables" folder to see "dbo.YourTableName" (the table which you have created now).

So the bottomline is when accessing a table name in SQL Server 2005, it shld be done like this:

ServerName.DatabaseName.SchemaName.TableName


Please note that if we want we can create our own schema and then assign the tables to it. Also when a new user is being created we can assign the default schema to them. This merits a separate discussion by itself so would write on it sometime soon.

Technorati tags:

Comments

Popular posts from this blog

Registry manipulation from SQL

Registry Manupulation from SQL Server is pretty easy. There are 4 extended stored procedure in SQL Server 2000 for the purpose of manupulating the server registry. They are: 1) xp_regwrite 2) xp_regread 3) xp_regdeletekey 4) xp_regdeletevalue Let us see each one of them in detail! About xp_regwrite This extended stored procedure helps us to create data item in the (server’s) registry and we could also create a new key. Usage: We must specify the root key with the @rootkey parameter and an individual key with the @key parameter. Please note that if the key doesn’t exist (without any warnnig) it would be created in the registry. The @value_name parameter designates the data item and the @type the type of the data item. Valid data item types include REG_SZ and REG_DWORD . The last parameter is the @value parameter, which assigns a value to the data item. Let us now see an example which would add a new key called " TestKey ", and a new data item under it called TestKeyValue :

Screen scraping using XmlHttp and Vbscript ...

I wrote a small program for screen scraping any sites using XmlHttp object and VBScript. I know I haven't done any rocket science :) still I thought of sharing the code with you all. XmlHttp -- E x tensible M arkup L anguage H ypertext T ransfer P rotocol An advantage is that - the XmlHttp object queries the server and retrieve the latest information without reloading the page. Source code: < html > < head > < script language ="vbscript"> Dim objXmlHttp Set objXmlHttp = CreateObject("Msxml2.XMLHttp") Function ScreenScrapping() URL == "UR site URL comes here" objXmlHttp.Open "POST", url, False objXmlHttp.onreadystatechange = getref("HandleStateChange") objXmlHttp.Send End Function Function HandleStateChange() If (ObjXmlHttp.readyState = 4) Then msgbox "Screenscrapping completed .." divShowContent.innerHtml = objXmlHttp.responseText End If End Function </ script > < head > < body > &l

Script table as - ALTER TO is greyed out - SQL SERVER

One of my office colleague recently asked me why we are not able to generate ALTER Table script from SSMS. If we right click on the table and choose "Script Table As"  ALTER To option would be disabled or Greyed out. Is it a bug? No it isn't a bug. ALTER To is there to be used for generating modified script of Stored Procedure, Functions, Views, Triggers etc., and NOT for Tables. For generating ALTER Table script there is an work around. Right click on the table, choose "Modify" and enter into the design mode. Make what ever changes you want to make and WITHOUT saving it right click anywhere on the top half of the window (above Column properties) and choose "Generate Change Script". Please be advised that SQL Server would drop actually create a new table with modifications, move the data from the old table into it and then drop the old table. Sounds simple but assume you have a very large table for which you want to do this! Then it woul