SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Issue with OSQL


Issue with OSQL

Author
Message
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
Hi Friends,

Requirement : i have a table which contains around 40 columns of which name is a column which i use to segregate the data because the contains the data for multiple names. Extract data for a NAME and store that in a file in windows directory.

what i did:

i have written a stored proc to which i send NAME as a parameter.The proc extracts all the data for that NAME. So, i have used OSQL utility to generate the output in a file( should be generated in a windows directory). I have around 30 to 40 NAMES.So, i have a cursor which loops through the NAMES and calls OSQL for each and every NAME.OSQL command is assigned to a string variable and that string is passed as a parameter to xp_cmdshell.

Issue: whatever i wrote after OSQL command(execute xp_cmdshell), not getting executed.Also, if i pass 40 NAMES , only 20 to 30 files are getting created and rest of the files are not getting generated at all.


Please advise your valuable suggestion.
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
adding to the below, i am using sql server 2005.
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15097 Visits: 14396
A few things:

1) osql is deprecated in SQL 2005. Consider switching to use sqlcmd or bcp for this work.
2) accessing the file system from T-SQL can create some scenarios that are difficult to debug. Consider switching from running this process in a cursor in T-SQL to something in SSIS or PowerShell so you can move away from using xp_cmdshell. Moving in this direction could make item 1 moot since SSIS and PowerShell both have the ability to write to files natively, i.e. without the help of SQL Server command lines tools.
3) We cannot see what you see. Please post the code that includes the cursor and the call to xp_cmdshell so we can see too. Make sure to cleanse any sensitive information from the code before posting including items in code comments and object names that may reveal company or personal information.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
sorry for the delay as i am not in town.

below is the code snippet. on an average this cursor has to generate around 90 files and this cursor runs every 15min.
----------------------------------------
declare my_cursor CURSOR for
select distinct WRS.f1 , WRS.c1 from tab2 WCB
inner join tab1 WRS on WCB.c1=WRS.c1

OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @f1, @c1
WHILE @@FETCH_STATUS = 0
BEGIN

set @nvchProcedure = 'proc name'
select @ts = convert(varchar, getdate(),112)+replace(convert(varchar, getdate(),108),':','') -- suffix the file name with datetimestamp

select @ff1 = @f1 + @ts + '.psv'
insert into tab3 (c1,f1,ff1) values (@c1,@f1,@ff1)

SELECT @cmd = 'osql -S ' + @@servername +' -E -n -h-1 -w2500 -q "SET NOCOUNT ON EXEC ' + @HostDB + '.dbo.' + @nvchProcedure +' '''+@c1 +''''+',''''+'" -o "'+ @nvchFileLocation +@f1+@ts+'.psv'

EXEC master..xp_cmdshell @cmd

FETCH NEXT FROM my_cursor
INTO @f1, @c1
END

CLOSE my_cursor
DEALLOCATE my_cursor
----------------------------------------
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15097 Visits: 14396
What comes to mind initially is that @cmd is sometimes ending up NULL due to @c1 or @t1 (coming from the cursor-select) being NULL since anything+NULL=NULL. Try adding some logging or some RAISERROR commands with severity=10 to see what @cmd is set to when it stops generating files.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
there would be values in C1 and T1 for sure.They never be null
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15097 Visits: 14396
Does @f1 ever have a character in it that would be considered invalid in a Windows file name?

Do you know what I mean when I say add calls to "RAISERROR with severity 10"?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
No, @f1 has only underscore as the special character reamining all are letters/digits.

Could you please explain more on RAISEERROR with sev 10?
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15097 Visits: 14396
Compassionate (3/5/2013)
No, @f1 has only underscore as the special character reamining all are letters/digits.

Could you please explain more on RAISEERROR with sev 10?

You can add calls to RAISERROR with a severity of 10 or less which will send an informational message to the output stream.

Here is a generic call that sends a literal string to the output stream:

RAISERROR('Custom informational message.', 10, 1);



Another way to call RAISERROR that might be useful in your case is to use tokens to send variable values to the output stream, like this:

RAISERROR('@f1 = %s', 10, 1, @f1);

What's happening in the above statement is that the value of @f1 is being substituted into the place of the token %s, which means "a string value", within the message before it is sent to the output stream There is a different token for numbers, %d, and some other tokens for different data types as well. Unfortunately there is no token for date or time values.

You can read more here:

RAISERROR (Transact-SQL) - SQL Server 2005

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search