Showing posts with label reference. Show all posts
Showing posts with label reference. Show all posts

Wednesday, March 7, 2012

OSQL Output File Garbage

Everybody,

I've been doing a lot of on-line research and cannot find
any reference to the exact problem I'm having.

Let me preface this question with the fact that I'm coming
from an Oracle background so my approach may not be the best
way to tackle this. However, from the research I have done
this approach seems reasonable. Also, I know about the
undocumented procedure sp_MSforeachtable. That can give me a
result similar to what I'm looking for but the format of the
output is not what I need.

Now the problem. I'm trying to write a reusable script to give
me a list of all the tables in a database that have 1 or more rows.
My approach is to a BAT file (see script 1 below) that calls OSQL
twice, once to call a SQL script (see script 2 below) that uses the
Information_Schema views to generate the SELECT COUNT(*) statements
and fill in all the tables names in the database, write this to a
temporary output file and the second OSQL command to read the
temporary output file and generate me the results formatted the
way I need.

The result of the first OSQL run is correct EXCEPT for 1> 2> 3> 4> 5>
6> 7> 8> 9> 10> 11> 12> 13> garbage at the beginning of the file.
Because of this garbage the 2nd OSQL command blows up! Anyone have
any idea what is generating this garbage?

If I manually edit out the garbage and then just run the 2nd OSQL
command
I get similar garbage in the final result file (see 2nd result file
below).

In Query Analyzer, when I run the GET_TABLE_COUNT.SQL Script manually
then take its output and copy and paste it to a new query window and
run that it works OK except for generating lots of blank lines where
the result of the tables that have zero rows are. I am suppressing
headings but am still getting the blank lines but at least it works!

Any ideas anybody? Thanks For Any Help
FYI -- SQL Server 2000 with SP3a.
Bob

================== Script 1 - BAT File to Call OSQL ===============

@.echo off
@.echo ************************************************** *************
@.echo .
@.echo get_table_count.bat
@.echo .
@.echo Before you run this script change to the drive and directory
@.echo where the input SQL script is located!
@.echo .
@.echo Input parameters:
@.echo 1) SQL Server userid
@.echo .
@.echo You will be prompted twice for your password!
@.echo .
@.echo The output is written to file TABLE_COUNT_RESULT.TXT
@.echo .
@.echo ************************************************** *************
pause
osql -U %1 -S devkc-db -d C3T_Architecture -i get_table_count.sql -o
temp_table_count_query.txt -h-1 -w500
osql -U %1 -S devkc-db -d C3T_Architecture -i
temp_table_count_query.txt -o table_count_result.txt -h-1 -w500
del temp_table_count_result.txt
@.echo on

================================================== ====================

================ Script 2 - GET_TABLE_COUNT.SQL Script ===============

set nocount on
select 'set nocount on'
select 'select ''Table Name Count'''
select 'select ''========== ====='''
select 'select '''
+ table_name
+ ''', count(*) from '
+ table_name
+ ' having count(*) > 0 '
from information_schema.tables
where table_type = 'BASE TABLE'
order by table_name

================================================== ====================

============ Partial Result of 1st OSQL Run ==========================

1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> set nocount on

select 'Table Name Count'

select '========== ====='

select 'ACT_ASSERTION_RULE', count(*) from ACT_ASSERTION_RULE having
count(*) > 0
select 'ACT_ASSOC', count(*) from ACT_ASSOC having count(*) > 0
select 'ACT_DOC', count(*) from ACT_DOC having count(*) > 0

================================================== ====================

============ Partial Result of @.nd OSQL Run ==========================

1> 2> 3> 4> ... I edited out the intervening numbers for this message
... 664> 665> 666> 667> Table Name Count

========== =====

... I edited out lots of blank lines in the result for this message
before I get to the first table with 1 or more rows ...

ARCH 6

================================================== ====================You can remove numbering with the '-n' OSQL parameter.

However, you might consider using dynamic SQL to accomplish the task.
Example below:

SET NOCOUNT ON
CREATE TABLE #TableRowCounts
(
TableName nvarchar(261) NOT NULL,
TableRowCount bigint NOT NULL
)
DECLARE
@.TableName nvarchar(261),
@.SqlStatement nvarchar(500)
DECLARE TableList CURSOR
LOCAL FAST_FORWARD READ_ONLY FOR
SELECT
QUOTENAME(TABLE_SCHEMA) +
'.' +
QUOTENAME(TABLE_NAME) AS TableName
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) +
'.' +
QUOTENAME(TABLE_NAME)),
'IsMSShipped') = 0
OPEN TableList
WHILE 1 = 1
BEGIN
FETCH NEXT FROM TableList INTO @.TableName
IF @.@.FETCH_STATUS = -1 BREAK
SET @.SqlStatement =
N'INSERT INTO #TableRowCounts
SELECT ''' + @.TableName + N''', COUNT(*)
FROM ' + @.TableName + N' WITH (NOLOCK)'
EXEC (@.SqlStatement)
END
CLOSE TableList
DEALLOCATE TableList

SELECT *
FROM #TableRowCounts
WHERE TableRowCount > 0
ORDER BY TableName

--
Hope this helps.

Dan Guzman
SQL Server MVP

--------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index...epartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--------

"Bob" <rms@.robertsiegel.net> wrote in message
news:91f443f8.0311141523.10db27e5@.posting.google.c om...
> Everybody,
> I've been doing a lot of on-line research and cannot find
> any reference to the exact problem I'm having.
> Let me preface this question with the fact that I'm coming
> from an Oracle background so my approach may not be the best
> way to tackle this. However, from the research I have done
> this approach seems reasonable. Also, I know about the
> undocumented procedure sp_MSforeachtable. That can give me a
> result similar to what I'm looking for but the format of the
> output is not what I need.
> Now the problem. I'm trying to write a reusable script to give
> me a list of all the tables in a database that have 1 or more rows.
> My approach is to a BAT file (see script 1 below) that calls OSQL
> twice, once to call a SQL script (see script 2 below) that uses the
> Information_Schema views to generate the SELECT COUNT(*) statements
> and fill in all the tables names in the database, write this to a
> temporary output file and the second OSQL command to read the
> temporary output file and generate me the results formatted the
> way I need.
> The result of the first OSQL run is correct EXCEPT for 1> 2> 3> 4> 5>
> 6> 7> 8> 9> 10> 11> 12> 13> garbage at the beginning of the file.
> Because of this garbage the 2nd OSQL command blows up! Anyone have
> any idea what is generating this garbage?
> If I manually edit out the garbage and then just run the 2nd OSQL
> command
> I get similar garbage in the final result file (see 2nd result file
> below).
> In Query Analyzer, when I run the GET_TABLE_COUNT.SQL Script manually
> then take its output and copy and paste it to a new query window and
> run that it works OK except for generating lots of blank lines where
> the result of the tables that have zero rows are. I am suppressing
> headings but am still getting the blank lines but at least it works!
> Any ideas anybody? Thanks For Any Help
> FYI -- SQL Server 2000 with SP3a.
> Bob
> ================== Script 1 - BAT File to Call OSQL ===============
> @.echo off
> @.echo ************************************************** *************
> @.echo .
> @.echo get_table_count.bat
> @.echo .
> @.echo Before you run this script change to the drive and directory
> @.echo where the input SQL script is located!
> @.echo .
> @.echo Input parameters:
> @.echo 1) SQL Server userid
> @.echo .
> @.echo You will be prompted twice for your password!
> @.echo .
> @.echo The output is written to file TABLE_COUNT_RESULT.TXT
> @.echo .
> @.echo ************************************************** *************
> pause
> osql -U %1 -S devkc-db -d C3T_Architecture -i get_table_count.sql -o
> temp_table_count_query.txt -h-1 -w500
> osql -U %1 -S devkc-db -d C3T_Architecture -i
> temp_table_count_query.txt -o table_count_result.txt -h-1 -w500
> del temp_table_count_result.txt
> @.echo on
> ================================================== ====================
> ================ Script 2 - GET_TABLE_COUNT.SQL Script ===============
> set nocount on
> select 'set nocount on'
> select 'select ''Table Name Count'''
> select 'select ''========== ====='''
> select 'select '''
> + table_name
> + ''', count(*) from '
> + table_name
> + ' having count(*) > 0 '
> from information_schema.tables
> where table_type = 'BASE TABLE'
> order by table_name
> ================================================== ====================
>
> ============ Partial Result of 1st OSQL Run ==========================
> 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> set nocount on
> select 'Table Name Count'
> select '========== ====='
> select 'ACT_ASSERTION_RULE', count(*) from ACT_ASSERTION_RULE having
> count(*) > 0
> select 'ACT_ASSOC', count(*) from ACT_ASSOC having count(*) > 0
> select 'ACT_DOC', count(*) from ACT_DOC having count(*) > 0
> ================================================== ====================
>
> ============ Partial Result of @.nd OSQL Run ==========================
> 1> 2> 3> 4> ... I edited out the intervening numbers for this message
> ... 664> 665> 666> 667> Table Name Count
> ========== =====
> ... I edited out lots of blank lines in the result for this message
> before I get to the first table with 1 or more rows ...
> ARCH 6
> ================================================== ====================|||Dan,

Thanks for the answers. I completely missed the -n option in the BOL.

I also like your alternative. Someone I work with suggested using a
temporary table then just selecting what I want but your approach
seems even more sophicated. I'm out of the office today so haven't
had a chance to try either answer but will as soon as possible.

Thanks so much,
Bob

"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message news:<Rkftb.917$Rk5.180@.newsread1.news.atl.earthlink.net >...
> You can remove numbering with the '-n' OSQL parameter.
> However, you might consider using dynamic SQL to accomplish the task.
> Example below:
> SET NOCOUNT ON
> CREATE TABLE #TableRowCounts
> (
> TableName nvarchar(261) NOT NULL,
> TableRowCount bigint NOT NULL
> )
> DECLARE
> @.TableName nvarchar(261),
> @.SqlStatement nvarchar(500)
> DECLARE TableList CURSOR
> LOCAL FAST_FORWARD READ_ONLY FOR
> SELECT
> QUOTENAME(TABLE_SCHEMA) +
> '.' +
> QUOTENAME(TABLE_NAME) AS TableName
> FROM INFORMATION_SCHEMA.TABLES
> WHERE OBJECTPROPERTY(
> OBJECT_ID(
> QUOTENAME(TABLE_SCHEMA) +
> '.' +
> QUOTENAME(TABLE_NAME)),
> 'IsMSShipped') = 0
> OPEN TableList
> WHILE 1 = 1
> BEGIN
> FETCH NEXT FROM TableList INTO @.TableName
> IF @.@.FETCH_STATUS = -1 BREAK
> SET @.SqlStatement =
> N'INSERT INTO #TableRowCounts
> SELECT ''' + @.TableName + N''', COUNT(*)
> FROM ' + @.TableName + N' WITH (NOLOCK)'
> EXEC (@.SqlStatement)
> END
> CLOSE TableList
> DEALLOCATE TableList
> SELECT *
> FROM #TableRowCounts
> WHERE TableRowCount > 0
> ORDER BY TableName
>
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> --------
> SQL FAQ links (courtesy Neil Pike):
> http://www.ntfaq.com/Articles/Index...epartmentID=800
> http://www.sqlserverfaq.com
> http://www.mssqlserver.com/faq
> --------

osql command reference?

Hi!
I have been looking for a complete command reference to osql. A document
that describes all possible SQL commands that you acn use. Unfortunately,
I seem too stupid :/
I haven't found:
* the complete desciption of BACKUP and RESTORE
* a way to show the table definitions.
TIA,
Stefan
At command prompt, run the following command and it will show you all
switches available for OSQL.
OSQL /?
Also see the topic "osql utility" in SQL Server 2000 Books Online.
Similarly, SQL Server Books Online has complete documentation on BACUP and
RESTORE commands.
sp_help will give you table definitions and you can generate table creation
scripts in Enterprise Manager.
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Stefan M. Huber" <looseleaf@.gmx.net> wrote in message
news:opsbdo75yjs9ddfw@.news.individual.de...
Hi!
I have been looking for a complete command reference to osql. A document
that describes all possible SQL commands that you acn use. Unfortunately,
I seem too stupid :/
I haven't found:
* the complete desciption of BACKUP and RESTORE
* a way to show the table definitions.
TIA,
Stefan
|||On Mon, 19 Jul 2004 11:09:11 +0100, Narayana Vyas Kondreddi
<answer_me@.hotmail.com> wrote:

> At command prompt, run the following command and it will show you all
> switches available for OSQL.
> OSQL /?
These, I know, thanks

> Also see the topic "osql utility" in SQL Server 2000 Books Online.
> Similarly, SQL Server Books Online has complete documentation on BACUP
> and RESTORE commands.

> sp_help will give you table definitions and you can generate table
> creation scripts in Enterprise Manager.
Thanks, that helped me to find my way through. I found an online reference
at
<http://manuals.sybase.com/onlinebook...sg1250e/sqlug/>.
Stefan
|||Hi,
Books online is the best option to learn all commands and usage.
http://www.microsoft.com/sql/techinf...2000/books.asp
* the complete desciption of BACKUP and RESTORE
See backup and Restore in books online
* a way to show the table definitions.
sp_help <table_name>
* OSQL
See OSQL in books online
Thanks
Hari
MCDBA
"Stefan M. Huber" <looseleaf@.gmx.net> wrote in message
news:opsbdo75yjs9ddfw@.news.individual.de...
> Hi!
> I have been looking for a complete command reference to osql. A
document
> that describes all possible SQL commands that you acn use. Unfortunately,
> I seem too stupid :/
> I haven't found:
> * the complete desciption of BACKUP and RESTORE
> * a way to show the table definitions.
> TIA,
> Stefan
|||The online reference you found is for Sybase and is not valid for Microsoft
SQL Server. Follow Hari's link.
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Stefan M. Huber" <looseleaf@.gmx.net> wrote in message
news:opsbdq5udms9ddfw@.pluto...
On Mon, 19 Jul 2004 11:09:11 +0100, Narayana Vyas Kondreddi
<answer_me@.hotmail.com> wrote:

> At command prompt, run the following command and it will show you all
> switches available for OSQL.
> OSQL /?
These, I know, thanks

> Also see the topic "osql utility" in SQL Server 2000 Books Online.
> Similarly, SQL Server Books Online has complete documentation on BACUP
> and RESTORE commands.

> sp_help will give you table definitions and you can generate table
> creation scripts in Enterprise Manager.
Thanks, that helped me to find my way through. I found an online reference
at
<http://manuals.sybase.com/onlinebook...sg1250e/sqlug/>.
Stefan
|||On Mon, 19 Jul 2004 15:57:16 +0530, Hari Prasad
<hari_prasad_k@.hotmail.com> wrote:

> Hi,
> Books online is the best option to learn all commands and usage.
> http://www.microsoft.com/sql/techinf...2000/books.asp
> * the complete desciption of BACKUP and RESTORE
> See backup and Restore in books online
> * a way to show the table definitions.
> sp_help <table_name>
> * OSQL
> See OSQL in books online
Thanks!
And while my other link isn't for MSDE, most of the things discussed there
work in MSDE as well
Stefan
|||osql is primarily a utility for running Transact-SQL statements on an
instance of SQL Server, including MSDE 2000. The primary reference for most
of the statements you can run using osql is the Transact-SQL Reference in
the SQL Server 2000 Books Online.
You can download the latest version of the SQL Server 2000 Books Online
from:
http://www.microsoft.com/sql/techinf...2000/books.asp
The latest version of the SQL Server 2000 Books Online is also published in
the MSDN Library at:
http://msdn.microsoft.com/library/?u...asp?frame=true
These are topics about running osql that are in the copy of the Books Online
in MSDN:
http://msdn.microsoft.com/library/de...asp?frame=true
http://msdn.microsoft.com/library/?u...asp?frame=true
This is the start of the Transact-SQL Reference in the MSDN copy of the
Books Online:
http://msdn.microsoft.com/library/de...asp?frame=true
Alan Brewer [MSFT]
Lead Programming Writer
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights