Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Friday, March 30, 2012

OUTPUT - help with please

I am using a dynamic t-sql string in proc1 to execute proc2, which returns an int variable named @.Fatal_Error back to proc1.

When I execute proc2 I use the syntax:

EXEC @.SQL @.Params

@.SQL is the Proc Name (varchar) and @.Params is the parameter string (nvarchar).

If I include the @.Fatal_Error variable in the dynamic creation of the @.Params string the returning value from Proc2 is unable to convert int to nvarchar.

I have declared @.Fatal_Error in proc1 as int and tried to add to the end of my dynamic t-sql EXEC but I still get 'Cannot convert int to nvarchar' .

Please help - I'm beginning to pull out hair! :-)

Thanks!

Here' s the syntax I tried when just passing it at the end of the EXEC call:

EXEC @.SQL @.Param_List = @.Fatal_Error

AND I also tried:

EXEC @.SQL @.Param_List + ' '+@.Fatal_Error+' '

You have to use the sp_executesql for parameterized dynamic sql,

Code Snippet

Declare @.idParm as int;

Declare @.nameParm as varchar(100);

Declare @.dynamicSql as nvarchar(1000);

Declare @.dynamicParamDef as nvarchar(1000);

Set @.idParm = 2;

Set @.nameParm = 'sysobjects'

Set @.dynamicSql = N'Select * from sysobjects where id=@.id or name=@.name'

Set @.dynamicParamDef = N'@.id as int, @.name as varchar(100)'

Exec sp_executesql @.dynamicSql, @.dynamicParamDef, @.idParm, @.nameParm

|||

I quote from BOL(look for sp_executesql, building statement at runtime)

"

Transact-SQL supports the following methods of building SQL statements at run time in Transact-SQL scripts, stored procedures, and triggers:

Use the sp_executesql system stored procedure to execute a Unicode string. sp_executesql supports parameter substitution similar to the RAISERROR statement.

Use the EXECUTE statement to execute a character string. The EXECUTE statement does not support parameter substitution in the executed string."|||...and Manivannan prove it|||

Thank you all for your assistance. I will give it a whirl.

I was able to finally execute with EXEC @.SQL @.Params, @.Fatal_Error = @.Fatal_Error

However, I'm sure this will come back to bite me in the long run..

Thank you ALL for your quick replies!

|||

@.SQL is my stored procedure name

@.Param_List is the list of enumerated parameters

SET @.SQL = @.SQL + IsNull(@.Param_List,'');

EXECUTE sp_executesql @.SQL;

Worked like a charm.

Thanks!

Sandy

Tuesday, March 20, 2012

Otain error details from a DTS package through T-SQL. Was(xp_sendmail on script

SQL Server 2000

Good day all,

I've been asked to create a DTS package that will execute 2 other DTS packages (yeah, I know... personally I want to write a sp for it *shrug*) and send an e-mail out reporting a failure and failure details.

So I have a couple of questions for you;

How do I pick up the error number (and description?) from a failed DTS and pass it to my sendmail task?
Is it possible to send anything other than plain text e-mails?
Can I change the e-mail priority to "high"?

I appreciate any help you can offer me :)USE master
GO

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'S' AND name = 'sp_dtsFailureEmail') BEGIN
DROP PROCEDURE sp_dtsFailureEmail
END
GO

CREATE PROCEDURE sp_dtsFailureEmail
@.dtsName varchar(64)
, @.errorNumber int = 0
AS

IF @.errorNumber <> 0 BEGIN
DECLARE @.message varchar(2000)
, @.subject varchar(255)
, @.errorDesc nvarchar(255)

SET @.errorDesc = (SELECT description FROM sysmessages WHERE error = @.errorNumber)
SET @.subject = '***DTS FAILURE*** ' + @.dtsname + ' ***DTS FAILURE***'
SET @.message = 'DTS Package:' + Char(9) + Char(9) + @.dtsName
+ Char(13) + 'Failure Date:' + Char(9) + Char(9) + Convert(varchar, GetDate(), 0)
+ Char(13) + 'Error Number:' + Char(9) + Char(9) + Convert(varchar, @.errorNumber)
+ Char(13) + 'Error Description:' + Char(9) + @.errorDesc

PRINT ''
PRINT @.subject
PRINT ''
PRINT @.message
PRINT ''

EXEC xp_sendmail
@.recipients = '<enter your e-mail address>'
, @.subject = @.subject
, @.message = @.message
END
ELSE BEGIN
PRINT 'No Error'
END
GO

SELECT 1/0

EXEC sp_dtsFailureEmail '<enter dtsName>', @.@.Error

DROP PROCEDURE sp_dtsFailureEmail

I'm getting close, but I still don't know how to pass the error number from a DTS to... anywhere!!|||EXEC xp_cmdshell 'DTSRun /S "SQL-XXXX-XXXXX" /N "<dtsname>" /G "{5A14F80F-8EE2-477D-BE95-DC90A343D2A3}" /W "0" /E '
SELECT @.@.Error

Returns no error number when the DTS fails... So this suggests that this cannot be done!
Any ideas?|||As you know I know stuff all about DTS but you are not getting much help here.

1) Should you change the title of the thread? The problem does not really have anything to do with xp_sendmail.
2) Have you tried:

Declare @.error_return AS INT
EXEC @.error_return = xp_cmdshell 'DTSRun /S "SQL-XXXX-XXXXX" /N "<dtsname>" /G "{5A14F80F-8EE2-477D-BE95-DC90A343D2A3}" /W "0" /E '
SELECT @.error_return ??
3) Have you considered setting up a global variable, trapping the error in the DTS script and setting the global. Then test the global when the DTS script has finished.

HTH|||1) Wilco... my q2&3 were sp_sendmail though..

2) I'm just working on that! It returns a bit value declaring whether there was an error or not.

3) Never used global variables in DTS before - will look up if I run into a dead end with point 2.

Here's my current working code.

USE master
GO

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'S' AND name = 'sp_dtsFailureEmail') BEGIN
DROP PROCEDURE sp_dtsFailureEmail
END
GO

CREATE PROCEDURE sp_dtsFailureEmail
@.dtsName varchar(64)
AS

DECLARE @.message varchar(8000)
, @.subject varchar(255)
, @.errorDesc nvarchar(255)

-- SET @.errorDesc = (SELECT description FROM sysmessages WHERE error = @.errorNumber)
SET @.subject = '***DTS FAILURE*** ' + @.dtsname + ' ***DTS FAILURE***'
SET @.message = 'DTS Package:' + Char(9) + Char(9) + @.dtsName
+ Char(13) + 'Failure Date:' + Char(9) + Char(9) + Convert(varchar, GetDate(), 0)
-- + Char(13) + 'Error Number:' + Char(9) + Char(9) + Convert(varchar, @.errorNumber)
-- + Char(13) + 'Error Description:' + Char(9) + @.errorDesc

PRINT ''
PRINT @.subject
PRINT ''
PRINT @.message
PRINT ''

-- EXEC xp_sendmail
-- @.recipients = '<enter your email>'
-- , @.subject = @.subject
-- , @.message = @.message
-- END
GO

DECLARE @.err bit
DECLARE @.dts varchar(64)
DECLARE @.cmd varchar(1024)

SET @.dts = 'test'
SET @.cmd = 'DTSRun /S "SQL-LIVE-IT038" /N "' + @.dts + '" /W "0" /E'

EXEC @.err = xp_cmdshell @.cmd
IF @.err <> 0 BEGIN
EXEC sp_dtsFailureEmail @.dts
END

DROP PROCEDURE sp_dtsFailureEmail

Returns:

DTSRun: Loading...
DTSRun: Executing...
DTSRun OnStart: DTSStep_DTSExecuteSQLTask_1
DTSRun OnError: DTSStep_DTSExecuteSQLTask_1, Error = -2147217900 (80040E14)
Error string: Divide by zero error encountered.
Error source: Microsoft OLE DB Provider for SQL Server
Help file:
Help context: 0

Error Detail Records:

Error: -2147217900 (80040E14); Provider Error: 8134 (1FC6)
Error string: Divide by zero error encountered.
Error source: Microsoft OLE DB Provider for SQL Server
Help file:
Help context: 0

DTSRun OnFinish: DTSStep_DTSExecuteSQLTask_1
DTSRun: Package execution complete.
NULL

***DTS FAILURE*** test ***DTS FAILURE***

DTS Package: test
Failure Date: Sep 5 2007 12:16PM

This raises another question: Is it possible to retrieve the output information from xp_cmdshell?|||This raises another question: Is it possible to retrieve the output information from xp_cmdshell?You mean stuff printed to the window? No.

How come? Are you hoping to get the details?

As said I don't know DTS but can you get the details within the package? If so then you can write all this to a table and check that.|||You mean stuff printed to the window?

Yep.

How come? Are you hoping to get the details?

Yep.

Basically the aim is to e-mail a group of users with the error details of a DTS package. But I'm startign to think it's more trouble than it's worth ;)|||GeorgeV on DTS: Basically the aim is to e-mail a group of users with the error details of a DTS package. But I'm startign to think it's more trouble than it's worth ;)

Strong candidate for my new sig ;)

That, for me, is DTS through and through.|||Yeeeeappp...
I'd love to rewrite the whole script in T-SQL; but can't...

I don't know how to do absolutely everything the DTS does
It's classed as a "major change" and I really cba going through all that just to do it.

A colleague of mine wrote a couple of hefty SSIS packages a while back which took a few CSV's from a 3rd party product and transformed them into our databases... All was fine until the column order changed on the CSV file. So even SSIS does the simplest of jobs badly.|||A colleague of mine wrote a couple of hefty SSIS packages a while back which took a few CSV's from a 3rd party product and transformed them into our databases... All was fine until the column order changed on the CSV file. So even SSIS does the simplest of jobs badly.To be fair unless you write bullet proof dynamic sql or have some sort of config file T-SQL will fair no better. I imagine Pat would start muttering something about "contracts" upon reading this. You change the contract between two systems and you can't really expect things to run smoothly.|||Actually, to resolve this an interim DTS package was created which produces a "clean" file. DTS packages remember the mapping.

The 3rd party product cost a total of $99 - we got what we paid for :p

Saturday, February 25, 2012

osql

I tried to issue this utitlity with T-SQL in my machine, but it asked me the
system administrator's password, how do I know this password at my local SQL
Server ?Alan wrote:
> I tried to issue this utitlity with T-SQL in my machine, but it asked
> me the system administrator's password, how do I know this password
> at my local SQL Server ?
Specify your loing credentials when you call osql. See BOL for more
information.
--
David Gugick
Imceda Software
www.imceda.com|||Alan
You may be able to use the -E switch to connect using a trusted connection.
ie. OSQL -Sservername -E
For a full list of OSQL switches refer to the osql Utility in SQL Server
Books on Line or run OSQL /? from the command line.
--
--
Peter Ward
WARDY IT Solutions
--
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried to issue this utitlity with T-SQL in my machine, but it asked me
the
> system administrator's password, how do I know this password at my local
SQL
> Server ?
>|||YOu may also use a standard SQL login like
osql -SServername -UyourSQLlogin -PPassword
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"P. Ward" <peter@.online.wardyit.com> wrote in message
news:%23aB1XQ9yEHA.2192@.TK2MSFTNGP14.phx.gbl...
> Alan
> You may be able to use the -E switch to connect using a trusted
connection.
> ie. OSQL -Sservername -E
> For a full list of OSQL switches refer to the osql Utility in SQL Server
> Books on Line or run OSQL /? from the command line.
> --
> --
> Peter Ward
> WARDY IT Solutions
> --
> "Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
> news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> > I tried to issue this utitlity with T-SQL in my machine, but it asked me
> the
> > system administrator's password, how do I know this password at my local
> SQL
> > Server ?
> >
> >
>

osql

I tried to issue this utitlity with T-SQL in my machine, but it asked me the
system administrator's password, how do I know this password at my local SQL
Server ?
Alan wrote:
> I tried to issue this utitlity with T-SQL in my machine, but it asked
> me the system administrator's password, how do I know this password
> at my local SQL Server ?
Specify your loing credentials when you call osql. See BOL for more
information.
David Gugick
Imceda Software
www.imceda.com
|||Alan
You may be able to use the -E switch to connect using a trusted connection.
ie. OSQL -Sservername -E
For a full list of OSQL switches refer to the osql Utility in SQL Server
Books on Line or run OSQL /? from the command line.
--
Peter Ward
WARDY IT Solutions
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried to issue this utitlity with T-SQL in my machine, but it asked me
the
> system administrator's password, how do I know this password at my local
SQL
> Server ?
>
|||YOu may also use a standard SQL login like
osql -SServername -UyourSQLlogin -PPassword
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"P. Ward" <peter@.online.wardyit.com> wrote in message
news:%23aB1XQ9yEHA.2192@.TK2MSFTNGP14.phx.gbl...
> Alan
> You may be able to use the -E switch to connect using a trusted
connection.
> ie. OSQL -Sservername -E
> For a full list of OSQL switches refer to the osql Utility in SQL Server
> Books on Line or run OSQL /? from the command line.
> --
> --
> Peter Ward
> WARDY IT Solutions
> --
> "Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
> news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> the
> SQL
>

osql

I tried to issue this utitlity with T-SQL in my machine, but it asked me the
system administrator's password, how do I know this password at my local SQL
Server ?Alan wrote:
> I tried to issue this utitlity with T-SQL in my machine, but it asked
> me the system administrator's password, how do I know this password
> at my local SQL Server ?
Specify your loing credentials when you call osql. See BOL for more
information.
David Gugick
Imceda Software
www.imceda.com|||Alan
You may be able to use the -E switch to connect using a trusted connection.
ie. OSQL -Sservername -E
For a full list of OSQL switches refer to the osql Utility in SQL Server
Books on Line or run OSQL /? from the command line.
--
Peter Ward
WARDY IT Solutions
--
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried to issue this utitlity with T-SQL in my machine, but it asked me
the
> system administrator's password, how do I know this password at my local
SQL
> Server ?
>|||YOu may also use a standard SQL login like
osql -SServername -UyourSQLlogin -PPassword
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"P. Ward" <peter@.online.wardyit.com> wrote in message
news:%23aB1XQ9yEHA.2192@.TK2MSFTNGP14.phx.gbl...
> Alan
> You may be able to use the -E switch to connect using a trusted
connection.
> ie. OSQL -Sservername -E
> For a full list of OSQL switches refer to the osql Utility in SQL Server
> Books on Line or run OSQL /? from the command line.
> --
> --
> Peter Ward
> WARDY IT Solutions
> --
> "Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
> news:Od7KzB6yEHA.2016@.TK2MSFTNGP15.phx.gbl...
> the
> SQL
>