473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OSQL Output File Garbage

Bob
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_MSforeachtab le. 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_Sch ema 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_RES ULT.TXT
@echo .
@echo *************** *************** *************** *************** ***
pause
osql -U %1 -S devkc-db -d C3T_Architectur e -i get_table_count .sql -o
temp_table_coun t_query.txt -h-1 -w500
osql -U %1 -S devkc-db -d C3T_Architectur e -i
temp_table_coun t_query.txt -o table_count_res ult.txt -h-1 -w500
del temp_table_coun t_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_sch ema.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_R ULE 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

=============== =============== =============== =============== ==========
Jul 20 '05 #1
2 13499
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_SCH EMA.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....partmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

"Bob" <rm*@robertsieg el.net> wrote in message
news:91******** *************** ***@posting.goo gle.com...
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_MSforeachtab le. 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_Sch ema 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_RES ULT.TXT
@echo .
@echo *************** *************** *************** *************** ***
pause
osql -U %1 -S devkc-db -d C3T_Architectur e -i get_table_count .sql -o
temp_table_coun t_query.txt -h-1 -w500
osql -U %1 -S devkc-db -d C3T_Architectur e -i
temp_table_coun t_query.txt -o table_count_res ult.txt -h-1 -w500
del temp_table_coun t_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_sch ema.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_R ULE 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

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

Jul 20 '05 #2
Bob
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" <da*******@nosp am-earthlink.net> wrote in message news:<Rk******* ********@newsre ad1.news.atl.ea rthlink.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_SCH EMA.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....partmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
23364
by: Jay Chan | last post by:
I am trying to use a command line program to run a stored procedure that generates output in a comma-delimitted format. Somehow, ISQL or OSQL always wrap the lines at 256 characters. I believe this has something to do with the column width switch (-w). But enlarging the column width to 800 characters max still doesn't help. The following is a stored procedure that is essentially doing what my stored procedure is doing: create procedure...
4
1884
by: topjun | last post by:
using osql to apply SPs in mutiple threads Hello, I got a weird problem when I was using osql to apply scripts for msde database in multiple threads mode. Sometime 2 sps were missing during the whole apply process, sometime not, and seems like only those two SPs met the problem. No error was appeared. Did anyone meet same problem before? Or any possible solutions? Thank you very much!
1
3330
by: Alan Zhong | last post by:
hi, i had been removed the row counts and the column spaces... but i failed to remove the extra tabs between rows of data return from the osql output. how to detect the row delimiter? i noticed i can use bcp to have a more decent output file, but i don't want to do too much of file read-write. the programming enviroment is quite easy to "capture" the output from the comand prompt. secondary, char(252), 253, 254, 255 is not being read...
24
4750
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source OLTP DB to a the target Operational Data Store. This is still in development so both DB's reside on the same machine for convenience. The stored proc runs successfully from within Query analyzer and this holds true on the following platforms: XP...
1
3037
by: vbnetrookie | last post by:
Hi all, This is my first batch file and I want to query a database and output it in a textfile. Up to now that works, the only problem is the formatting in the text file. It's all screewed up...lines aren't lined up and columns aren't right....how do you format the result from a query to a text file. Here's my code: osql.exe -S MYMACHINE -w 30 -E -d Demo2 -Q "select * from Title where frequency = 'monthly'" -o "C:\output.txt"
0
1583
by: dlewis0323 | last post by:
Hi everyone, I'm using osql to generate a pipe-delimited output file, that is potentially up to 500 characters wide. No matter what I set the -w parameter to in my batch file, the output file still wraps at position 192. Is there an additional parameter that I can use to allow up to 500 characters, or is this a limitation of osql?? Any advice would be greatily appreciated! Thanks, Dave Lewis
7
2283
by: gopal | last post by:
Hi, I have a console application whose purpose is to run the OSQL utility from my console application. When my application is run, the OSQL utility is started and it has to prompt for Database UserName & Password .
5
2056
by: gopal | last post by:
Hi I am running a OSQL utility from my console application . iam trying to install a huge SQL file of 5MB, i debugged my code and the application is not responding at the following line of code psi.FileName = "osql.exe"; psi.Arguments = osqlArgs;
1
2027
by: kishjeff | last post by:
is there some way to activate osql from a c++ app and stream the output to my program?
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.