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
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
hi, i had been removed the row counts and the column spaces... but ifailed to remove the extra tabs between rows of data return from theosql output.how to detect the row delimiter?i noticed i can use bcp to have a more decent output file, but i don'twant to do too much of file read-write. the programming enviroment isquite easy to "capture" the output from the comand prompt.secondary, char(252), 253, 254, 255 is not being read by the commandprompt, for example, 253 turns into 132 when i decode the input.thankx.from alan.
I'm running a query with osql, and I'm trying to get some clean output that is comma delimited. So far my line looks like this:osql -E -n -d mydb -i custom.qry -o "c:output esults.csv" -h-1 -s ","This works off a table with only two columns. I'm still left with a lot of extra spaces between the first column output and second column output, and at the bottom the text "(50 rows affected)".So instead of this:data1a (lots of spaces here) ,data1bdata2a (lots of spaces here) ,data2bdata3a (lots of spaces here) ,data3b(50 rows affected)I want to see this:data1a,data1bdata2a,data2bdata3a,data3bThanks very much for any help. :)
I am trying to use a command line program to run a stored procedurethat generates output in a comma-delimitted format. Somehow, ISQL orOSQL always wrap the lines at 256 characters. I believe this hassomething to do with the column width switch (-w). But enlarging thecolumn width to 800 characters max still doesn't help. The followingis a stored procedure that is essentially doing what my storedprocedure is doing:create procedure MyTest asset ansi_padding onset nocount ondeclare @sTest varchar(300)-- Output three lines. Each line has 259 characters.select @sTest = "1234 6789 ... 1234 6789"print @sTestselect @sTest = "1 3 5 7 9 ... 1 3 5 7 9"print @sTestselect @sTest = "1 3 5 7 9 ... 1 3 5 7 9"print @sTestset nocount offreturn( 0 )I invoke this stored procedure using this command:isql -SMyDbSrv -E -dMyDb -w800 -x800 -h-1 -n -Q"exec MyTest"-oMyTest.txt-- or --osql -SMyDbSrv -E -dMyDb -w800 -h-1 -n -Q"exec MyTest" -oMyTest.txtBut they have the same problem. The output lines all wrap around at256 characters.Strangely, if I store the result in a temporary table, and then useSELECT to output the result from the temporary table, I will not havethat problem. Seem like the "-w" switch only works for output fromtables, but not for output coming from PRINT. Unfortunately, usingthis approach has another set of problems (one blank space in front ofeach line, "number-of-rows affected" shows up at the bottom).Therefore, I would like to stick with using PRINT statements to outputthe result.Please suggest a way to fix this line-wrapping-around problem.Thanks.Jay Chan
the 'myscript.sql' file updates a table with another table of the exact number of rows.
When the scheduled task runs, the 'mytrans.log' file shows 0 rows were affected.
If I run this myself from the command line, 22,000 rows are affected which is correct.
I am guessing there is some sort of permission/authentication issue here. I am sending the right username/password for SQL and for windows to run the task.
osql -E declare @cmd nvarchar(1000) declare @cmd2 nvarchar(1000) declare @state1 varchar(100) declare @message varchar(100) set @message = '' -- Build command to determine state of SQLSERVERAGENT service on Master Server SET @CMD = 'create table #state (state varchar(2000))' + char(10) + 'declare @cmdx varchar(1000)' + char(10) + 'insert into #state EXEC master..xp_servicecontrol ''''QueryState'''', ''''SQLSERVERAGENT''''' + + char(10) + 'select @state=state from #state' + char(10) + 'drop table #state' -- Build command to execute command that determines state of service being monitored set @cmd2 = 'declare @state varchar(100)' + char(10) + 'exec ' + rtrim(@@servername) + '.master.dbo.sp_executesql N''' + @CMD + ''',' + 'N''@state varchar(100) out'',' + '@state out' + char(10) + 'set @state1 = @state' -- Execute command and return state of service being monitored exec master.dbo.sp_executesql @cmd2,N'@state1 varchar(100) out',@state1 out -- Is the service that was monitored not IF (UPPER(@state1) <> 'RUNNING.') --if @state1 <1 'Running.' begin -- Display message that primary monitor is down select @message = @message+char(13)+ @@servername + ' -' + 'Sql Server Agent Not Running'+char(13) print 'Master server "' + rtrim(@@servername) + '" for monitoring is not available.' exec master.dbo.xp_smtp_sendmail
It works fine when I run it on the command line prompt. And I receive a mail , if the server agent is running.
But when I save it as bat file and try to run , it stops and doesnot even give an error.
Hi all,This is my first batch file and I want to query a database and outputit in a textfile. Up to now that works, the only problem is theformatting in the text file. It's all screewed up...lines aren't linedup and columns aren't right....how do you format the result from aquery to a text file. Here's my code:osql.exe -S MYMACHINE -w 30 -E -d Demo2 -Q "select * from Title wherefrequency = 'monthly'" -o "C:output.txt"Thanks....JMT
Hi guys I need to apply a sql script file on Sql server 2000 by .net 2.0 program. but on the current running machine, there is not Osql.exe file. Do you guys know how to execute the sql file without the command? Is there any way provided in .net library ? Thanks for you response!
Hi,I have dumped a very large database from mysql (using mysqldump program)as a raw sql file. The reason was, convert this database to a MSSQLdatabase. Since mysqldump creates the file as raw sql file with thedatabase-table structures and the data in it, I thought using OSQL commandline utilities should work to out this whole database in MSSQL server.I have run this command from command line:osql -u sa -i mysqldump.sqlIt is going since yesterday. It has been almost 36 hours that it'sstarted. And in the mssql server, I see no database created yet. On thescreen of the command line, I see bunch of numbers are going in order. Iassume they are row numbers of the tables processed. But, if it is doing it,then where is it saving all this data ? I have checked the tempdb, pub db,other dbs, and I see no tables related to the database I am inserting. Willit populate it at the and of the job ? Or, am I doing something wrong here?Regards.Murtix.
hi evryone I have a question! in my ASP.NET page I use a SqlDataAdapter object for working on my database first I declare it as a global vaiable(SqlDataAdapter objDataAdapter;) then I construct it in my Page_Load procedure like this (objDataAdapter=new SqlDataAdapter(strSql,objConnention)) then when I want to use it in my other procedures an error tells me that your object does not exist for solving this error I should construct my objDataAdapter in evry procedure using it. but I think such variables should not be erased from Heap automatically(with Garbage Collection) because it has a reference to Stack. I mean the global variable... any opinion ? Thanks
Hi all! I´m getting a very unpleasant result when I use "SELECT * FROM <many-columns-table (40+)>". Each query row appears with overwritten columns (not all but some of them)... also outside EM window !!! There is no data corruption on EM nor Windows, all programs remains running with no other sign but garbage overwritten... garbage dissapears after window refreshing. Just 1 month ago I made a 2-day full reinstall of my computer, so I don´t know what program could install an offending module/DLL/??? buy I´m sure it was working before. I hate that! Have you ever noticed something like that? Do you think is a DLL problem? Do you know a turn around? (I don´t want to reinstall all again!!!) Actually I´ve tried reinstalling SP4, SP5 on WS, uninstalling/reinstalling Client with no luck... Thanks, Cesar.
hi, My database server had hung up recently, and I had to restart it. After checking the log I found some garbage entries in it. has anyone encountered similar errors? harshal.
I was testing my packages today and my packages were running sucessfully when I didnt have any valid data in the flat file. One the reason was as not rows were returned from the flat file none of the validation script components returned any err.
How do I count the # of rows which were read from flat file from the package and continue only if there is more than one row.
I tried using conditional split but as I wont have the row count value availble till the dataflow task runs this didnt help.
Is it best for me to have two dataflow tasks one resturns the count of records from flat file and the other starts if there are any rows. Now my problem is if I have rows to process how to I transfer the flatfile data to validate from DataFlowTask1 to DataFlowTask2?
I have a script task whcih counts the rows and decides to the TaskResult but once the TaskResult is sucess how do I use the values read in DataFlowTask1?
Folks I have recently had the misfortune of moving to SQL Server Managment Studio as per our upgrade to SQL 2005. There is no doubt that SQL Server Management is a major disappoinment compared to Enterprise manager. The UI is the biggest disaster I have ever seen. Getting rid of the SQL Query analyzer tool was a TERRIBLE idea. The UI is buggy and unfriendly. It is a major POS. I hope someone in the SQL Dev team is listening / reading this post (hellooooo anybody homeee......) Now that I have vented my frustration ... I wanted to know any atlernatives to the SQL Server Management Studio. There are plenty of 3rd Party tools out there I wanted to find out from this forum which popular 3rd Party tools are most developers using Thanks for bearing with me.
I built a SSIS(writing out to a flat file ) in 32 bit machine and it woks fine . But however when I deploy to the produciton server(64 bit) the SSIS writes out garbage data . After some research I found out that the problem with the 32 bit OS and 64 bit OS problem.What is my next step. Am I out of luck that now I will have to redesing the SSIS in 64 bit?
SET @RowCnt = 1 SET @date = CONVERT(CHAR(10),GETDATE(),110) SET @ArchPath = '\D$EDATAWorkFoldersSendSendData' SELECT @TotalRows = count(*) FROM table1 --select @ArchPath
WHILE (@RowCnt <= @TotalRows) BEGIN SELECT @AccountNumber = AccountNumber, @output_filename FROM table1 WHERE Identity_Number = @RowCnt --PRINT @AccountNumber --test SELECT @sql = N'bcp "SELECT h.HeaderText, d.RECORD FROM table2 d INNER JOIN table3 h ON d.HeaderID = h.HeaderID WHERE d.ccountNumber = ''' + @AccountNumber+'''" queryout "'+@ArchPath+ @output_filename + '.txt" -T -c' --PRINT @sql EXEC master..xp_cmdshell @sql SELECT @RowCnt = @RowCnt + 1 END
In my SSIS Package, I have to write my [FileHeaderRecord] row, then my [BatchHeaderRecord] row, then my details. How can I do this in a SQL Server Query? When I try my SSIS, my file looks like so..
FHTEST 00000208262015Â Â Â Â Â Â Â Â Â Â Â Â BH000208262015Â Â
I want my BH, Batch Header data, to appear on a new row in the file.Do I have to build a dynamic query to do this?Is there any trick in SSIS to do something like this?I did try creating separate Data Flow Tasks to Query the [FileHeaderRecord] and then use a Flat File Destination and then another Data Flow Task to Query the [BatchHeaderRecord] and use a Flat File Destination again NOT overwriting the file.
I am transferring data from an OLEDB source to a Flat File Destination and I want the column width for all of the output columns to 30 (max width amongst the columns selected), but that is not refected in the Fixed Width Flat File that got created. The outputcolumnwidth seems to be the same as the inputcolumnwidth. Is there any other setting that I am possibly missing or is this a possible defect?
I am trying to create an ssis package with dynamic csv file as output. and out format contains query output.
sample file name:
Unique identifier + query output + systemdate();
The expression is looking like this.
@[User::FilePath] + @[User::FileName] + ".CSV"
-- user filepath is a variable from ssis package. File name is the output from SQL query. using script task i have assigned the values to @[User::FileName] .
When I debugged the script task the value getting properly but same variable am using for Flafile destination. but its not working.
Hi all! My question is quite simple I think... I am directing the result of my query to an output file my problem is how to set the rowsize of my output file to more than 256 (that is the default maximum)? My query result is quite long per row (assuming one column) in query anlyzer, I can easily adjust the result to more than 256 but the result in the output file is truncated because the ouput is more than 256 characters per row...how can I set (in the code) the length of the row of the result in the output file... thanks for the help... this is quite urgent...
I'm not new to sql server, but making my first experience with xml in sql server 2005.
I have a query like this (based on <Table> with neccessary data):
SELECT TAG, PARENT, <columns...> FROM <Table> FOR XML EXPLICIT
This query creates a xml file exactly as i need it when i execute it in Management Studio. Well, with one exception. It does not write the <xml...> tag at the beginning of the xml file. But i'm sure i get that in there somewho else. What i need to do now is get that output to a file on disk. And that's where my problem starts.
I tried SQLCMD within Management Studio, but that doesn't accept the ':XML ON' tag and ignores it. the resulting file written is not usable, as it also contains query summary information.
I have a header row and a footer row and a bunch of detail data I have managed to split up into temp db tables, know i need to know what would I need to do to write the header , detail data and footer to the same file after all my validations have completed , this would be a different file then the one I originally imported all the data from (append to text delimited file). Idealy I would only like to change the header and footer from the original file.
Any ideas - I am thinking of writing a application that does this for me and just executing it from SSIS but I would really like to stick with standard SSIS components first before starting to write my own stuff.
Is it possible to send the output of a query to a text file in a stored procedure? When I run stored procedure in Query Analyzer I am able to do that and I am wondering if this is possible in a automated way?
Hi, Is it the way use T-SQL to select text data from table and add them to the file on a HD, but save the information in the file without changes anything that was in the file before, another words without rewriting, just add?
I have a query something like this: select "bcp EISAT_08_18.."+name +" OUT C:"+ name+".TXT -c -t -SCJACOBI" from sysobjects where type = 'U' ORDER BY NAME When I run the above query I want to output the result of the query to a file. Can someone help me on that?
Hi all, When I run a query in the sql query analyzer I need to write the output of that query in to another file. In Oracle its spool. Can someone help me on this please. Thank you!!!