Showing posts with label contains. Show all posts
Showing posts with label contains. Show all posts

Friday, March 23, 2012

Number of rows of all the tables in the database

Is there a way to get a list of all the table and the
number of rows it contains?
Thanks
Anna> Is there a way to get a list of all the table and the
> number of rows it contains?
http://www.aspfaq.com/2428
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Anna,
> Is there a way to get a list of all the table and the
> number of rows it contains?
You can also open a cursor and get the counts of each table in a
loop. This procedure allows you to do a bit of filtering of the
tables.
use Northwind
go
create procedure get_rowcounts (
@.table sysname = '%'
) as
declare @.sql varchar(1000)
create table #Tmp (table_name sysname, row_count int)
declare csrTable cursor local fast_forward for
select table_name from information_schema.tables
where table_name like @.table
and table_type = 'BASE TABLE'
order by table_name
open csrTable
fetch next from csrTable into @.table
while (@.@.fetch_status = 0) begin
set @.Sql = 'select ''' + @.table +
''', count(*) from [' + @.table + ']'
-- print @.sql
insert #tmp exec (@.sql)
fetch next from csrTable into @.table
end
close csrTable
deallocate csrTable
select row_count, table_name from #tmp
go
exec get_rowcounts 'Customers'
exec get_rowcounts 'Order%'
exec get_rowcounts
go
drop procedure get_rowcounts
-- Linda|||Thanks
>--Original Message--
>Anna,
>> Is there a way to get a list of all the table and the
>> number of rows it contains?
>You can also open a cursor and get the counts of each
table in a
>loop. This procedure allows you to do a bit of filtering
of the
>tables.
>
>use Northwind
>go
>create procedure get_rowcounts (
> @.table sysname = '%'
>) as
>declare @.sql varchar(1000)
>create table #Tmp (table_name sysname, row_count int)
>declare csrTable cursor local fast_forward for
>select table_name from information_schema.tables
>where table_name like @.table
>and table_type = 'BASE TABLE'
>order by table_name
>open csrTable
>fetch next from csrTable into @.table
>while (@.@.fetch_status = 0) begin
> set @.Sql = 'select ''' + @.table +
> ''', count(*) from [' + @.table + ']'
>-- print @.sql
> insert #tmp exec (@.sql)
> fetch next from csrTable into @.table
>end
>close csrTable
>deallocate csrTable
>select row_count, table_name from #tmp
>go
>exec get_rowcounts 'Customers'
>exec get_rowcounts 'Order%'
>exec get_rowcounts
>go
>drop procedure get_rowcounts
>
>-- Linda
>
>.
>|||Hi, you can try to use select count(*) from tablename.
Yanling
>--Original Message--
>Is there a way to get a list of all the table and the
>number of rows it contains?
>Thanks
>Anna
>.
>sql

Monday, March 19, 2012

number formats (again!)

Hi

I'm having a problem with displaying my reports through my application at runtime. One of the formula fields on the report contains the id of the record I'm displaying and if the record's Verified field = True, it also shows a string. The formula is this:

If {?PubNoVer} = "Yes" then
if {View_TM_Get_BookID_BY_Surname.Verified} = True Then
{View_TM_Get_BookID_BY_Surname.BookID} & " Verified"
Else
{View_TM_Get_BookID_BY_Surname.BookID} & " "
Else
" "

In the report preview window, this all looks fine but running it through my application made any four digit id look wrong because it added a thousand separator and decimal places e.g. 1,512.00

In a previous post on another forum, a user very kindly showed me that using ToText on the ID field would get rid of the decimal places, so that's fine, but I still have the thousand separator and can't get rid of it!

Does anyone have any ideas?just format the field


right click on field, select format field, go to number that and select the format u want to display in|||just format the field


right click on field, select format field, go to number that and select the format u want to display in

Yeah, I did that but it only shows the correct format in the preview window, when running the report through the application, it shows the thousand separator and the decimal places even though i'd specified for that not to happen!

OK, I've found half an answer on another site. To get rid of both the decimal places and the thousands separators you use:

ToText({fieldname},0,"")

This works but because I'm also grouping on the id field, the ids still show with a thousands separator on the group tree at the side of the report! Anybody know how to format that?!|||You could create a formula like:

right('0000000000' & totext({fieldname}, 0, ""), 10)

and then group on that formula instead of the fieldname.
Trouble is you get a load of leading zeros in front of the number in the group tree.
You could limit this by finding the maximum length of the field with a whilereadingrecords formula and changing the formula to
right(replicatestring('0', {@.maxlen}) & totext({fieldname}, 0, ""), {@.maxlen})

You could use a space instead of a zero, but the numbers in the group tree won't line up, although they will still be in numeric order.

If anyone knows another way (apart from setting the PC's local settings) I'd like to know...|||You could create a formula like:

right('0000000000' & totext({fieldname}, 0, ""), 10)

and then group on that formula instead of the fieldname.
Trouble is you get a load of leading zeros in front of the number in the group tree.

Thanks JaganEllis. Instead of the formula you suggested, I used ToText({fieldname}, 0, "") and grouped on it and this worked perfectly!|||I can see how that helps with the format of the group tree data, but what about the group ordering? Unless all your number have the same number of digits, the order will be messed up (if important to you).
e.g. 300 will be after 2000 because the string '3' is > the string '2'|||I can see how that helps with the format of the group tree data, but what about the group ordering? Unless all your number have the same number of digits, the order will be messed up (if important to you).
e.g. 300 will be after 2000 because the string '3' is > the string '2'

Ah right... I see what you mean! I've used spaces instead of zeros. Thanks!

Friday, March 9, 2012

Nulling text data

I have a table that contains text data(XML formatted code). We have decided
on a strategy to archive some of this data and null it out. Out of a 220GB
database, this history table is 153GB. After setting the text field to null,
we haven't realized any space improvements. We reindex often. The previous
DBA suggested copying the rows out and renaming the destination table but I'm
sceptical that I can finish that in any conceivable downtime due to the row
count and general query performance when accessing text columns. Any ideas
on how to reclaim this space?On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
On SQL 2005, you could use index defrag to reclaim the space.
--
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
>I have a table that contains text data(XML formatted code). We have
>decided
> on a strategy to archive some of this data and null it out. Out of a 220GB
> database, this history table is 153GB. After setting the text field to
> null,
> we haven't realized any space improvements. We reindex often. The
> previous
> DBA suggested copying the rows out and renaming the destination table but
> I'm
> sceptical that I can finish that in any conceivable downtime due to the
> row
> count and general query performance when accessing text columns. Any
> ideas
> on how to reclaim this space?|||use DBCC SHRINKDATABASE command
"wei xiao [MSFT]" <weix@.online.microsoft.com> wrote in message
news:OMJ0736oFHA.3756@.TK2MSFTNGP09.phx.gbl...
> On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
> On SQL 2005, you could use index defrag to reclaim the space.
> --
> Wei Xiao [MSFT]
> SQL Server Storage Engine Development
> http://blogs.msdn.com/weix
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
> message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
> >I have a table that contains text data(XML formatted code). We have
> >decided
> > on a strategy to archive some of this data and null it out. Out of a
220GB
> > database, this history table is 153GB. After setting the text field to
> > null,
> > we haven't realized any space improvements. We reindex often. The
> > previous
> > DBA suggested copying the rows out and renaming the destination table
but
> > I'm
> > sceptical that I can finish that in any conceivable downtime due to the
> > row
> > count and general query performance when accessing text columns. Any
> > ideas
> > on how to reclaim this space?
>

Nulling text data

I have a table that contains text data(XML formatted code). We have decided
on a strategy to archive some of this data and null it out. Out of a 220GB
database, this history table is 153GB. After setting the text field to null,
we haven't realized any space improvements. We reindex often. The previous
DBA suggested copying the rows out and renaming the destination table but I'm
sceptical that I can finish that in any conceivable downtime due to the row
count and general query performance when accessing text columns. Any ideas
on how to reclaim this space?
On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
On SQL 2005, you could use index defrag to reclaim the space.
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
>I have a table that contains text data(XML formatted code). We have
>decided
> on a strategy to archive some of this data and null it out. Out of a 220GB
> database, this history table is 153GB. After setting the text field to
> null,
> we haven't realized any space improvements. We reindex often. The
> previous
> DBA suggested copying the rows out and renaming the destination table but
> I'm
> sceptical that I can finish that in any conceivable downtime due to the
> row
> count and general query performance when accessing text columns. Any
> ideas
> on how to reclaim this space?
|||use DBCC SHRINKDATABASE command
"wei xiao [MSFT]" <weix@.online.microsoft.com> wrote in message
news:OMJ0736oFHA.3756@.TK2MSFTNGP09.phx.gbl...
> On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
> On SQL 2005, you could use index defrag to reclaim the space.
> --
> Wei Xiao [MSFT]
> SQL Server Storage Engine Development
> http://blogs.msdn.com/weix
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.[vbcol=seagreen]
> "Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
> message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
220GB[vbcol=seagreen]
but
>

Nulling text data

I have a table that contains text data(XML formatted code). We have decided
on a strategy to archive some of this data and null it out. Out of a 220GB
database, this history table is 153GB. After setting the text field to null
,
we haven't realized any space improvements. We reindex often. The previous
DBA suggested copying the rows out and renaming the destination table but I'
m
sceptical that I can finish that in any conceivable downtime due to the row
count and general query performance when accessing text columns. Any ideas
on how to reclaim this space?On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
On SQL 2005, you could use index defrag to reclaim the space.
Wei Xiao [MSFT]
SQL Server Storage Engine Development
http://blogs.msdn.com/weix
This posting is provided "AS IS" with no warranties, and confers no rights.
"Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
>I have a table that contains text data(XML formatted code). We have
>decided
> on a strategy to archive some of this data and null it out. Out of a 220GB
> database, this history table is 153GB. After setting the text field to
> null,
> we haven't realized any space improvements. We reindex often. The
> previous
> DBA suggested copying the rows out and renaming the destination table but
> I'm
> sceptical that I can finish that in any conceivable downtime due to the
> row
> count and general query performance when accessing text columns. Any
> ideas
> on how to reclaim this space?|||use DBCC SHRINKDATABASE command
"wei xiao [MSFT]" <weix@.online.microsoft.com> wrote in message
news:OMJ0736oFHA.3756@.TK2MSFTNGP09.phx.gbl...
> On SQL 2000, the best thing to do is to bcp out/in to reclaim the space.
> On SQL 2005, you could use index defrag to reclaim the space.
> --
> Wei Xiao [MSFT]
> SQL Server Storage Engine Development
> http://blogs.msdn.com/weix
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Jeffrey K. Ericson" <JeffreyKEricson@.discussions.microsoft.com> wrote in
> message news:BA149A27-1E2F-460A-A798-E4061FCDD4E4@.microsoft.com...
220GB[vbcol=seagreen]
but[vbcol=seagreen]
>

Nullable Foreign Keys

Hi,

I'm making a system enhancement and have a database design Q.

The database contains a transactionitem table with a foreign key relationship to a Product Table. I need to extend the database schema to show that the TransactionItem.Product may also relate to another type of product grouping (depends on the sale type and this will be solely for reporting). Note. I can't link back using the TransactionItem.ProductID as this could belong to more than one group.

My Question really is "Is it Good practise to have foreign key column with null values". I estimate that 80% or transactionitem records would contain null for this colunm. If I use an intermediate table the relationship between my TransactionItem and my new (new) ProductGroup table would be 0 to one. I.E : this product is only sold in special circumstances - and although the product itself can belong to more than one group for this specific sale it only applies to one group.

Should I add a column to the TransactionItemTable as below.

Table - TransactionItem

TransactionItemID ProductID ProductVoucherID

1 500 2

tblProductVoucher

ProductVoucherID (Other attributes)

2 .......

- OR Create a new association table

Table - TransactionItem

TransactionItemID ProductID

1 500

Table - TransactionVoucherItem

TransactionVoucherItemID TransactionItem ProductVoucherID

1 1 2

tblProductVoucher

ProductVoucherID (Other attributes)

2 ......

Can you clarify one part about your second option? Wouldn't TransactionVoucherItem inherit its primary key from TransactionItem? i.e.it doesn't need its own TransactionVoucherItemId, does it? Rows in this table exist 1:1 with TransactionItem's when that transaction has an associated ProductVoucher it sounds like.

To answer your overall question, I'd look at how often queries that select TransactionItem's need to also know if there is an associated ProductVoucher. If you go the separate table route, then look at how many queries over TransactionItem now have to be 'left joined' with TransactionVoucherItem in order to pickup the ProductVoucherID column. If there are a lot of queries that require this, then it will probably be cheaper to use your first option.

Also, to help decide, look at the current rowsize of a TransactionItem and see what percentage increase it would be to add the nullable ProductVoucherID column. If this is your biggest table, and this increase is more than say 20% of the current rowsize, that increase might outweigh the cost of the joins mentioned above. (I think this is the basic tradeoff from a performance perspective.)

From a logical modeling perspective, I've used the optional foreign key approach (your option 1) before without any issues for my database. It's probably the first option I'd consider as its the simplest, but you'll have to measure to see if it has any negative tradeoffs (like the extra size mentioned above.)

HTH.

--Shaun

|||

>>My Question really is "Is it Good practise to have foreign key column with null values". I estimate that 80% or transactionitem records would contain null for this colunm. If I use an intermediate table the relationship between my TransactionItem and my new (new) ProductGroup table would be 0 to one. I.E : this product is only sold in special circumstances - and although the product itself can belong to more than one group for this specific sale it only applies to one group.<<<

To me this is not a matter of how many mihgt be null, but what the relationship between them is. If you have a one to many relationship between the grou and the transaciton item, then that would be the best route. However, if there is any possibility that you might have multiples, then the seperate table is the best approach. Then you could have >1 group per transaction.

Having a column being 80% null won't be too much of an issue, especially since you are talking about using this as a lookup value to display and group by rather than as something to search for. You could even add a value for does not apply and make it not-nullable, especially if you will have to do a coalesce on all of your reports later and say coalesce(reportGroup.name,'Does not apply')

|||How if the foreign key references the primary key? Can it still have null in value? I am encountering a similar problem with my database.

Nullable Foreign Keys

Hi,

I'm making a system enhancement and have a database design Q.

The database contains a transactionitem table with a foreign key relationship to a Product Table. I need to extend the database schema to show that the TransactionItem.Product may also relate to another type of product grouping (depends on the sale type and this will be solely for reporting). Note. I can't link back using the TransactionItem.ProductID as this could belong to more than one group.

My Question really is "Is it Good practise to have foreign key column with null values". I estimate that 80% or transactionitem records would contain null for this colunm. If I use an intermediate table the relationship between my TransactionItem and my new (new) ProductGroup table would be 0 to one. I.E : this product is only sold in special circumstances - and although the product itself can belong to more than one group for this specific sale it only applies to one group.

Should I add a column to the TransactionItemTable as below.

Table - TransactionItem

TransactionItemID ProductID ProductVoucherID

1 500 2

tblProductVoucher

ProductVoucherID (Other attributes)

2 .......

- OR Create a new association table

Table - TransactionItem

TransactionItemID ProductID

1 500

Table - TransactionVoucherItem

TransactionVoucherItemID TransactionItem ProductVoucherID

1 1 2

tblProductVoucher

ProductVoucherID (Other attributes)

2 ......

Can you clarify one part about your second option? Wouldn't TransactionVoucherItem inherit its primary key from TransactionItem? i.e.it doesn't need its own TransactionVoucherItemId, does it? Rows in this table exist 1:1 with TransactionItem's when that transaction has an associated ProductVoucher it sounds like.

To answer your overall question, I'd look at how often queries that select TransactionItem's need to also know if there is an associated ProductVoucher. If you go the separate table route, then look at how many queries over TransactionItem now have to be 'left joined' with TransactionVoucherItem in order to pickup the ProductVoucherID column. If there are a lot of queries that require this, then it will probably be cheaper to use your first option.

Also, to help decide, look at the current rowsize of a TransactionItem and see what percentage increase it would be to add the nullable ProductVoucherID column. If this is your biggest table, and this increase is more than say 20% of the current rowsize, that increase might outweigh the cost of the joins mentioned above. (I think this is the basic tradeoff from a performance perspective.)

From a logical modeling perspective, I've used the optional foreign key approach (your option 1) before without any issues for my database. It's probably the first option I'd consider as its the simplest, but you'll have to measure to see if it has any negative tradeoffs (like the extra size mentioned above.)

HTH.

--Shaun

|||

>>My Question really is "Is it Good practise to have foreign key column with null values". I estimate that 80% or transactionitem records would contain null for this colunm. If I use an intermediate table the relationship between my TransactionItem and my new (new) ProductGroup table would be 0 to one. I.E : this product is only sold in special circumstances - and although the product itself can belong to more than one group for this specific sale it only applies to one group.<<<

To me this is not a matter of how many mihgt be null, but what the relationship between them is. If you have a one to many relationship between the grou and the transaciton item, then that would be the best route. However, if there is any possibility that you might have multiples, then the seperate table is the best approach. Then you could have >1 group per transaction.

Having a column being 80% null won't be too much of an issue, especially since you are talking about using this as a lookup value to display and group by rather than as something to search for. You could even add a value for does not apply and make it not-nullable, especially if you will have to do a coalesce on all of your reports later and say coalesce(reportGroup.name,'Does not apply')

|||How if the foreign key references the primary key? Can it still have null in value? I am encountering a similar problem with my database.

Monday, February 20, 2012

NULL encountered in math

I need to subtract two values in a table. One of these values contains a
NULL entry. Each time I use the (Pledge - Credit) as 'Difference' I get the
value of NULL.
How do I get T-SQL to replace the NULL with zero?
I encountered this a long time ago while concatenating data. I was able to
use the command SET CONCAT_NULL_YIELDS_NULL OFF for strings. But I can't
find anything for numbers.
Thanks in advance
"Joseph" <Joseph@.discussions.microsoft.com> wrote in message
news:674BC95B-D16C-42B2-ACEA-7F8D799DD38E@.microsoft.com...
>I need to subtract two values in a table. One of these values contains a
> NULL entry. Each time I use the (Pledge - Credit) as 'Difference' I get
> the
> value of NULL.
> How do I get T-SQL to replace the NULL with zero?
> I encountered this a long time ago while concatenating data. I was able
> to
> use the command SET CONCAT_NULL_YIELDS_NULL OFF for strings. But I can't
> find anything for numbers.
> Thanks in advance
You can use COALESCE or ISNULL. Example:
COALESCE(pledge,0) - COALESCE(credit,0) AS difference
David Portas
SQL Server MVP