Showing posts with label names. Show all posts
Showing posts with label names. Show all posts

Monday, March 26, 2012

How to get the list of table names in a SQL2005 database

How to get the list of table names in a SQL2005 database?There's a system catalog view named [sys.tables] that you can query. For more information on this look in SQL Books Online under "catalog views >> sys.objects".

Hope this helps,
Vaughn|||Thanks Vaughn. But when I query this view, it also returns some tables those are not created by myself such as dtproperties table and sysdiagrams table. How can I filter them out?|||

Depending on what exactly you're trying to accomplish you may want to filter out tables for which the "is_ms_shipped" column is "1" or you may want to join against the "sys.schemas" table and filter the results to certain schemas (e.g. owners).

Hope this helps,
Vaughn

|||The problem I have is that the "sysdiagrams" table, that is added when you try to make a diagram, is treated by the database just like a table I personally add. There are no settings that I can see that will identify the table as a microsoft table and not one of mine. I can put in code that excludes it, but are there any other tables that do this too that I should be checking for?sql

How to get the list of table names in a SQL2005 database

How to get the list of table names in a SQL2005 database?There's a system catalog view named [sys.tables] that you can query. For more information on this look in SQL Books Online under "catalog views >> sys.objects".

Hope this helps,
Vaughn|||Thanks Vaughn. But when I query this view, it also returns some tables those are not created by myself such as dtproperties table and sysdiagrams table. How can I filter them out?|||

Depending on what exactly you're trying to accomplish you may want to filter out tables for which the "is_ms_shipped" column is "1" or you may want to join against the "sys.schemas" table and filter the results to certain schemas (e.g. owners).

Hope this helps,
Vaughn

|||The problem I have is that the "sysdiagrams" table, that is added when you try to make a diagram, is treated by the database just like a table I personally add. There are no settings that I can see that will identify the table as a microsoft table and not one of mine. I can put in code that excludes it, but are there any other tables that do this too that I should be checking for?

Wednesday, March 21, 2012

how to get the all the column names of a table?

Can't figure out...

Given a database table name, how do I get all the column names of the table? Actually my task is more than that, what I want is the percentage of null values for each column. But there too many columns in the table and I don't want to type the column name one by one. How to achieve it?

For example, suppose I have a table TB which contains 50 columns C1 to C50. What I want to produce a stored-procedure that table the table name as input parameter and output the percentage of null values for each column C1 to C50, something like:

ColumnName NullPercentage
C1 14.28%

....
C50 6.89%

Thank you!

You can get the column names from system table SYSCOLUMNS.

You can use them to construct a dynamic SQL statement, and then use EXEC(@.string_variable) to run the query.

If you need further assistance, I can try to hunt up some code where I did this sort of thing to estimate the space consumed by a table. (Now with SQL Server 2005 you can find such space information, both for the table and its indexes, with "Properties" after right-clicking on the table name in SSMS.)

HTH.

Dan

|||I am fairy new to sql. If you could have some sample script that would be great! Thanks.
|||

I'll see if I can post the code tomorrow, Friday. Perhaps others here may beat me to it.

Dan

|||

Yes.. Here you go...

Hey Dan I never thought to beat you Buddy ..

Code Snippet

Declare @.Cols as Varchar(8000);

Declare @.PreparedCols as Varchar(8000);

Declare @.TableName as NVarchar(1000);

Select @.TableName = 'Orders'

Select

@.PreparedCols = ',Cast(Sum(Case When ? is null Then 1 Else 0 End)/Sum(1.0) * 100 as Numeric(5,2)) as [?]',

@.Cols = ''

Select

@.Cols = @.Cols + replace(@.PreparedCols, '?', name)

From

syscolumns

Where

id=Object_Id(@.TableName)

Exec ('Select ''NULL Value %'' as Comment' + @.Cols + ' From ' + @.TableName)

|||

Manivannen,

Not only did you beat me, but you provided a much-more compact solution!

I have never before built a dynamic SQL statement using a SELECT to append to an ever-growing SQL statement.

I like it!

Thanks for the education!

Where I tried it, though, I would like to note that I had to change @.COL to VARCHAR(MAX); it must have changed @.COL to NVARCHAR, since I could only print 4000 characters of it. It worked fine when I changed to VARCHAR(MAX). (Without the change the limit is around 40 columns, depending on the lengths of the column names.)

Dan

P.S. Is that enough exclamation points?

|||Beautiful!! Save me tons of time to check nulls.

I had to change to NVarchar(MAX) too, otherwise some of the tables will not work.

I marked both posts as answer.

Thanks!
|||

Thanks for your question, too!

I learn a lot by reading answers in this Forum.

Dan

how to get table structure?

Hi,
There is a command Describe in Oracle to get the table structure (column
names, types, etc. ).
Is there any similar command in SQL server?
Thanks,
Guangmingsp_help tablename
Word 2003 memory Leakage wrote:
> Hi,
> There is a command Describe in Oracle to get the table structure (column
> names, types, etc. ).
> Is there any similar command in SQL server?
> Thanks,
> Guangming|||Following will give you list of columns, data type, size etc.
sp_columns tablename
For more information, please have a look at:
http://www.aspfaq.com/show.asp?id=2177
"Word 2003 memory Leakage" wrote:
> Hi,
> There is a command Describe in Oracle to get the table structure (column
> names, types, etc. ).
> Is there any similar command in SQL server?
> Thanks,
> Guangming|||Both are working. sp_help returns more infor than sp_columns.
It seems they are much slower than describ in Oracle.
but it works.
Thanks,
"Absar Ahmad" wrote:
> Following will give you list of columns, data type, size etc.
> sp_columns tablename
> For more information, please have a look at:
> http://www.aspfaq.com/show.asp?id=2177
> "Word 2003 memory Leakage" wrote:
> > Hi,
> >
> > There is a command Describe in Oracle to get the table structure (column
> > names, types, etc. ).
> >
> > Is there any similar command in SQL server?
> >
> > Thanks,
> >
> > Guangming

How to get table list from a db? Thanks!

Hi Group!

I would like to get a list of all the table names from a database, can
anybody please tell me how to do that? Many thanks!See sp_tables and sysobjects in Books Online.

Simon|||Select table_name from information_schema.tables
where table_type='Base table' order by table_name

Madhivanan|||Hi, you can write it as:

SELECT name [Table Name]
FROM sysobjects
WHERE xtype = 'U'

Please let me know if this helped

best Regards,
Chandra
http://www.SQLResource.com/
http://chanduas.blogspot.com/
------------

*** Sent via Developersdex http://www.developersdex.com ***|||Chandra, It is better to use views and avoid using system tables

Madhivanan

Monday, March 19, 2012

How to get source IP address of a database connection

Hi,
Is there a way to find out the source IP address of a database connection?
In trace there are only netbios names available, while I need to log the IP
addresses that connect to the database.
Thx,
Jacek Skaznik
We store the MAC address in sysprocesses while the connection is there. If
you query master..sysprocesses you'll see it, but we don't map the MAC to
an IP.
Thanks,
Kevin McDonnell
Microsoft Corporation
This posting is provided AS IS with no warranties, and confers no rights.

How to get source IP address of a database connection

Hi,
Is there a way to find out the source IP address of a database connection?
In trace there are only netbios names available, while I need to log the IP
addresses that connect to the database.
Thx,
Jacek SkaznikWe store the MAC address in sysprocesses while the connection is there. If
you query master..sysprocesses you'll see it, but we don't map the MAC to
an IP.
Thanks,
Kevin McDonnell
Microsoft Corporation
This posting is provided AS IS with no warranties, and confers no rights.

Monday, March 12, 2012

How to get rid of dbo in field names?

Hello,
I just converted a website database from MS Access to SQL 2000, and many of
the pages are not working because SQL now wants fields to be ref'd as
"dbo.<<fieldname>>"... I've fixed this before, but cannot remember how... If
I cannot fix it through the database / connection, I'll have hundreds of
lines of code to modify.
Any suggestions?
Thanks in advance!!!!
--Jon
What are the login credentials of your application? Is it qualifying objects
with a different owner name? If so, s_changeobjectowner might help you.
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"code" <code@.community.nospam> wrote in message
news:3BD82441-93C0-4CC9-A466-DD1E76291669@.microsoft.com...
> Hello,
> I just converted a website database from MS Access to SQL 2000, and many
> of
> the pages are not working because SQL now wants fields to be ref'd as
> "dbo.<<fieldname>>"... I've fixed this before, but cannot remember how...
> If
> I cannot fix it through the database / connection, I'll have hundreds of
> lines of code to modify.
> Any suggestions?
> Thanks in advance!!!!
> --Jon

Wednesday, March 7, 2012

How to get names of all databases using sql statements?

How can i get names of all databases using sql statements or system stored procedures? Thanks in advance!How can i get names of all databases using sql statements or system stored procedures? Thanks in advance!

try this:
select name from master..sysdatabases|||I tried and the result is very good. Thanks a lot

how to get names and types of parameters of stored procedure

Hi everybody!
I need to get names and types of parameters of stored procedures from some
system tables.
( not from SQLDMO object ) . Does somebody know in what system table(s)
these data stored?
ThanksYou can find this information in the information_schema.parameters view.
--
Jacco Schalkwijk
SQL Server MVP
"Leo" <Leo@.discussions.microsoft.com> wrote in message
news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
> Hi everybody!
> I need to get names and types of parameters of stored procedures from
> some
> system tables.
> ( not from SQLDMO object ) . Does somebody know in what system table(s)
> these data stored?
> Thanks
>|||There is also a stored procedure, sp_proc_columns, I believe, that will
parse out that information for you.
Sincerely,
Anthony Thomas
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:%23dhtDeB6EHA.1564@.TK2MSFTNGP09.phx.gbl...
You can find this information in the information_schema.parameters view.
--
Jacco Schalkwijk
SQL Server MVP
"Leo" <Leo@.discussions.microsoft.com> wrote in message
news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
> Hi everybody!
> I need to get names and types of parameters of stored procedures from
> some
> system tables.
> ( not from SQLDMO object ) . Does somebody know in what system table(s)
> these data stored?
> Thanks
>|||Thanks to both of you :)
"AnthonyThomas" wrote:
> There is also a stored procedure, sp_proc_columns, I believe, that will
> parse out that information for you.
> Sincerely,
>
> Anthony Thomas
>
> --
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
> in message news:%23dhtDeB6EHA.1564@.TK2MSFTNGP09.phx.gbl...
> You can find this information in the information_schema.parameters view.
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "Leo" <Leo@.discussions.microsoft.com> wrote in message
> news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
> > Hi everybody!
> >
> > I need to get names and types of parameters of stored procedures from
> > some
> > system tables.
> > ( not from SQLDMO object ) . Does somebody know in what system table(s)
> > these data stored?
> >
> > Thanks
> >
> >
>
>

how to get names and types of parameters of stored procedure

Hi everybody!
I need to get names and types of parameters of stored procedures from some
system tables.
( not from SQLDMO object ) . Does somebody know in what system table(s)
these data stored?
Thanks
You can find this information in the information_schema.parameters view.
Jacco Schalkwijk
SQL Server MVP
"Leo" <Leo@.discussions.microsoft.com> wrote in message
news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
> Hi everybody!
> I need to get names and types of parameters of stored procedures from
> some
> system tables.
> ( not from SQLDMO object ) . Does somebody know in what system table(s)
> these data stored?
> Thanks
>
|||There is also a stored procedure, sp_proc_columns, I believe, that will
parse out that information for you.
Sincerely,
Anthony Thomas

"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
in message news:%23dhtDeB6EHA.1564@.TK2MSFTNGP09.phx.gbl...
You can find this information in the information_schema.parameters view.
Jacco Schalkwijk
SQL Server MVP
"Leo" <Leo@.discussions.microsoft.com> wrote in message
news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
> Hi everybody!
> I need to get names and types of parameters of stored procedures from
> some
> system tables.
> ( not from SQLDMO object ) . Does somebody know in what system table(s)
> these data stored?
> Thanks
>
|||Thanks to both of you
"AnthonyThomas" wrote:

> There is also a stored procedure, sp_proc_columns, I believe, that will
> parse out that information for you.
> Sincerely,
>
> Anthony Thomas
>
> --
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid > wrote
> in message news:%23dhtDeB6EHA.1564@.TK2MSFTNGP09.phx.gbl...
> You can find this information in the information_schema.parameters view.
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "Leo" <Leo@.discussions.microsoft.com> wrote in message
> news:D7FE4664-3595-4EC7-8EE3-C453B4823E74@.microsoft.com...
>
>

Friday, February 24, 2012

How to get list of database names and list of table names?

I want to get list of database names under a server,
and list of table names under a database.
How can I do this with query?
Probably a better way of doing this through InformationSchema but...
SELECT Name from master.dbo.sysdatabases
In each database
SELECT Name from sysobjects WHERE Type = 'U'
HTH
Rick Sawtell
MCT, MCSD, MCDBA
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
> I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>
|||-- All databases
SELECT name
FROM master..sysdatabases
-- All tables in a database
select name, *
FROM Northwind..sysobjects
where xtype = 'U'
Greetings, John
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
> I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>
|||You can also use the ANSI-standard INFORMATION_SCHEMA views:
SELECT CATALOG_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
OBJECTPROPERTY(OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)),
'IsMSShipped') = 0
Hope this helps.
Dan Guzman
SQL Server MVP
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
>I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>

How to get list of database names and list of table names?

I want to get list of database names under a server,
and list of table names under a database.
How can I do this with query?Probably a better way of doing this through InformationSchema but...
SELECT Name from master.dbo.sysdatabases
In each database
SELECT Name from sysobjects WHERE Type = 'U'
HTH
Rick Sawtell
MCT, MCSD, MCDBA
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
> I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>|||-- All databases
SELECT name
FROM master..sysdatabases
-- All tables in a database
select name, *
FROM Northwind..sysobjects
where xtype = 'U'
Greetings, John
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
> I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>|||You can also use the ANSI-standard INFORMATION_SCHEMA views:
SELECT CATALOG_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
OBJECTPROPERTY(OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)),
'IsMSShipped') = 0
--
Hope this helps.
Dan Guzman
SQL Server MVP
"GoodMorningSky" <sky@.forever21.com> wrote in message
news:%23oKl4ZJrEHA.516@.TK2MSFTNGP09.phx.gbl...
>I want to get list of database names under a server,
> and list of table names under a database.
> How can I do this with query?
>

How to get list of backup log files from msdb of standby server?

How do we retrieve log file names of primary backup from msdb of
standby database?
In case of primary server unavailable, I need to get the logfile names
from standby server for disater recovery test.
Thanks in advance
Tram
Msdb..backupfile gives you a list of data/log files that are backed up.
For logshipping, take a look at msdb..log_shipping_plan_databases.
-oj
"tram" <tram_e@.hotmail.com> wrote in message
news:1120079449.879078.169120@.o13g2000cwo.googlegr oups.com...
> How do we retrieve log file names of primary backup from msdb of
> standby database?
> In case of primary server unavailable, I need to get the logfile names
> from standby server for disater recovery test.
> Thanks in advance
> Tram
>

How to get list of backup log files from msdb of standby server?

How do we retrieve log file names of primary backup from msdb of
standby database?
In case of primary server unavailable, I need to get the logfile names
from standby server for disater recovery test.
Thanks in advance
TramMsdb..backupfile gives you a list of data/log files that are backed up.
For logshipping, take a look at msdb..log_shipping_plan_databases.
-oj
"tram" <tram_e@.hotmail.com> wrote in message
news:1120079449.879078.169120@.o13g2000cwo.googlegroups.com...
> How do we retrieve log file names of primary backup from msdb of
> standby database?
> In case of primary server unavailable, I need to get the logfile names
> from standby server for disater recovery test.
> Thanks in advance
> Tram
>

How to get list of backup log files from msdb of standby server?

How do we retrieve log file names of primary backup from msdb of
standby database?
In case of primary server unavailable, I need to get the logfile names
from standby server for disater recovery test.
Thanks in advance
TramMsdb..backupfile gives you a list of data/log files that are backed up.
For logshipping, take a look at msdb..log_shipping_plan_databases.
--
-oj
"tram" <tram_e@.hotmail.com> wrote in message
news:1120079449.879078.169120@.o13g2000cwo.googlegroups.com...
> How do we retrieve log file names of primary backup from msdb of
> standby database?
> In case of primary server unavailable, I need to get the logfile names
> from standby server for disater recovery test.
> Thanks in advance
> Tram
>

How to get list (text) of all tables and columns?

Is there a way using MS SQL Server and Enterprise Manager to get a text
document (or perhaps even a Word document) listing all table names,
column names, etc of a database?

--
Sugapablo
-----------
http://www.sugapablo.com <--music
http://www.sugapablo.net <--personal"Sugapablo" <russREMOVE@.sugapablo.com> wrote in message
news:vmu7b03j5uos68@.corp.supernews.com...
> Is there a way using MS SQL Server and Enterprise Manager to get a text
> document (or perhaps even a Word document) listing all table names,
> column names, etc of a database?
> --
> Sugapablo
> -----------
> http://www.sugapablo.com <--music
> http://www.sugapablo.net <--personal

It would probably be easier to use Query Analyzer instead, and get the
details you need from the INFORMATION_SCHEMA views (assuming you have SQL7
or 2000), eg.:

select TABLE_NAME, COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
order by TABLE_NAME, ORDINAL_POSITION

If it's a one-off task, then you can just cut and paste the results, but if
you need to do it regularly, then you could consider using SQLDMO from a
client application. That way you can create the whole Word document using VB
or something similar.

Simon|||Sugapablo <russREMOVE@.sugapablo.com> wrote in message news:<vmu7b03j5uos68@.corp.supernews.com>...
> Is there a way using MS SQL Server and Enterprise Manager to get a text
> document (or perhaps even a Word document) listing all table names,
> column names, etc of a database?

Hello,

To get a list of user tables use this query:

select name from ssysobjects where type = 'u'

And for a list of columns related for all objects (tables,views,sps)

select name, object_name(id) from sysindexes

The results could be pushed to a text document or any other ODBC
compliant datasource using DTS.

Regards,
-Manoj