Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Issue with OSQL Expand / Collapse
Author
Message
Posted Sunday, February 17, 2013 8:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
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.
Post #1420985
Posted Sunday, February 17, 2013 8:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
Points: 13, Visits: 31
adding to the below, i am using sql server 2005.
Post #1420986
Posted Monday, February 18, 2013 6:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,080, Visits: 12,571
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
Post #1421193
Posted Saturday, March 2, 2013 12:34 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
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
----------------------------------------
Post #1425829
Posted Saturday, March 2, 2013 4:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,080, Visits: 12,571
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
Post #1425843
Posted Monday, March 4, 2013 4:20 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
Points: 13, Visits: 31
there would be values in C1 and T1 for sure.They never be null
Post #1426172
Posted Monday, March 4, 2013 6:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,080, Visits: 12,571
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
Post #1426211
Posted Tuesday, March 5, 2013 4:11 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
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?
Post #1426726
Posted Tuesday, March 5, 2013 9:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,080, Visits: 12,571
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
Post #1426883
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse