Showing posts with label job. Show all posts
Showing posts with label job. Show all posts

Friday, March 30, 2012

Invoking SQL Server JOB created for a report schedule

Hi,
Instead of runing a report through the schedule that I have created, I am invoking the SQL Agent Job that has been created for the schedule of the report, using the system stored procedure sp_startjob. Is this a recommended approach? Are there drawbacks for this approach?
Subash

This is a perfectly reasonable solution. If you are just trying to fire a subscription you could also use the FireEvent method to invoke the subscription.

The only drawback is that this is not a supported scenario, which means the job could change in a SP or new release, however this seems unlikely.

Invoking ms access from a scheduled job in sql server 2000

Is it possible to run a mdb from a scheduled job in sql server? I simply want to run a mdb with a autoexec in it. Don't want to use microsoft scheduler....

Thanks!

To 'run a mdb...with a autoexec in it' requires a client application.

SQL Server is NOT a client application.

Wednesday, March 28, 2012

Invalid Token

A client's SQL 7.0 installation seems to be
deteriorating. We have several stored procs that run on
as job scheduled. It started with one stored proc that
has been running for months with no problems all of the
sudden occasionally "sticking". By sticking I mean the
stored proc never completes. The stored proc is question
run another stored proc as part of the process. It
appears to be sticking on the call of the embedded stored
proc. If I run the embedded stored proc seperately, it
never sticks. If I run the entire process manually, it
will stick "some" of the time. If I cancel the stored
proc when it does stick I get a "Invalid Token" error
from SQL.
I have made several changes to the way the procs are
called and have included jobs to back up the DB and run
maintenance before the calls - and that has "helped" but
not solved the problem.
Now there are other jobs that are "sticking". One is an
import routine that has not been changed in 5 years nor
has it ever stuck before this week.
I think these are all symptoms of a bigger database
problem and not problems in the stored procs or jobs at
all.
Any ideas?What does DBCC CheckDB tell you?
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
"Patrick" <anonymous@.discussions.microsoft.com> wrote in message
news:32e201c3b041$c3151f10$a601280a@.phx.gbl...
> A client's SQL 7.0 installation seems to be
> deteriorating. We have several stored procs that run on
> as job scheduled. It started with one stored proc that
> has been running for months with no problems all of the
> sudden occasionally "sticking". By sticking I mean the
> stored proc never completes. The stored proc is question
> run another stored proc as part of the process. It
> appears to be sticking on the call of the embedded stored
> proc. If I run the embedded stored proc seperately, it
> never sticks. If I run the entire process manually, it
> will stick "some" of the time. If I cancel the stored
> proc when it does stick I get a "Invalid Token" error
> from SQL.
> I have made several changes to the way the procs are
> called and have included jobs to back up the DB and run
> maintenance before the calls - and that has "helped" but
> not solved the problem.
> Now there are other jobs that are "sticking". One is an
> import routine that has not been changed in 5 years nor
> has it ever stuck before this week.
> I think these are all symptoms of a bigger database
> problem and not problems in the stored procs or jobs at
> all.
> Any ideas?
>|||CHECKDB found 0 allocation errors and 0 consistency errors
in database
>--Original Message--
>What does DBCC CheckDB tell you?
>--
>Geoff N. Hiten
>Microsoft SQL Server MVP
>Senior Database Administrator
>Careerbuilder.com|||Good, then there is nothing structurally wrong with your databases. The
'Invalid Token' message is likely a side effect of killing the process.
It may be that growth has changed the data so that SQL is generating
different query plans and they are now encountering locking and blocking
issues. I would check each query in the procedures and see what SQL thinks
the query plan is. You may find that modifying the index structure will
improve performance and decrease locking. There are also instances
(semi-rare) where the WITH RECOMPILE option may help. These almost always
involving a LIKE comparison that returns significantly different
percentages of the total rows in the table on subsequent calls to the proc.
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
"Patrick" <anonymous@.discussions.microsoft.com> wrote in message
news:01c501c3b04d$17c6f0f0$a101280a@.phx.gbl...
> CHECKDB found 0 allocation errors and 0 consistency errors
> in database
> >--Original Message--
> >What does DBCC CheckDB tell you?
> >
> >--
> >Geoff N. Hiten
> >Microsoft SQL Server MVP
> >Senior Database Administrator
> >Careerbuilder.com
>|||Thanks for your help - but I am still stuck.
Here is what I have now - I am tracing the SP - my trace
is set to trace Locks and SP starts and completions.
I run a SP that hangs sometimes (an End of Day routine
that is VERY long of course) and I am having trouble
getting it to fail on my test DB. If I cannot get it to
fail, I move some statements around in the SP a bit and
recreate the proc, and then it might hang. I finally was
able to get a good trace on the SP when it had hung (this
SP normally takes less than 10 secs to complete).
The trace confused me because it shows that all the
statements started and completed, including the last
statement call (the main SP call). However, in query
analyser, the query is still running.
The trace also shows no locks.
These are the last two statements in the trace -
sys_sp_eod is the stored proc that I am executing in QA
that is not ever completing. Any more ideas on how to
trace this down?
+SP:StmtCompleted MS SQL Query Analyzer
WMS_ADMIN 0 10 0 0
2276 15 12:52:52.253
+SP:Completed sys_sp_EOD MS SQL Query Analyzer
WMS_ADMIN 0
2276 15 12:52:52.253|||Does your procedure mail out results using SQL Mail or SQL Agent Mail? If
so, it is probably stuck on a hung MAPI interface call.
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
"Patrick" <anonymous@.discussions.microsoft.com> wrote in message
news:35dd01c3b05a$487e7c60$a601280a@.phx.gbl...
> Thanks for your help - but I am still stuck.
> Here is what I have now - I am tracing the SP - my trace
> is set to trace Locks and SP starts and completions.
> I run a SP that hangs sometimes (an End of Day routine
> that is VERY long of course) and I am having trouble
> getting it to fail on my test DB. If I cannot get it to
> fail, I move some statements around in the SP a bit and
> recreate the proc, and then it might hang. I finally was
> able to get a good trace on the SP when it had hung (this
> SP normally takes less than 10 secs to complete).
> The trace confused me because it shows that all the
> statements started and completed, including the last
> statement call (the main SP call). However, in query
> analyser, the query is still running.
> The trace also shows no locks.
>
> These are the last two statements in the trace -
> sys_sp_eod is the stored proc that I am executing in QA
> that is not ever completing. Any more ideas on how to
> trace this down?
> +SP:StmtCompleted MS SQL Query Analyzer
> WMS_ADMIN 0 10 0 0
> 2276 15 12:52:52.253
> +SP:Completed sys_sp_EOD MS SQL Query Analyzer
> WMS_ADMIN 0
> 2276 15 12:52:52.253
>|||No but maybe this is a clue? I have always suspected
something was amiss in our Receiving tables (this is a WMS
system) because of the SP is seems to get hung in and the
fact that the import SP is the new call that has been
hanging since yesterday - and it uses the receiving
tables. Here is something I found in the trace:
Event Class Text Application Name NT User
Name SQL User Name CPU Reads Writes Duration
Connection ID SPID Start Time
+Missing Column Statistics NO STATS:([Rcv_Doc].
[ORDER_DATE]) MS SQL Query Analyzer WMS_ADMIN
0 2276 15
13:49:59.770
+Missing Column Statistics NO STATS:([Rcv_Doc].
[ORDER_DATE]) MS SQL Query Analyzer WMS_ADMIN
0 2276 15
13:49:59.770
+Missing Column Statistics NO STATS:([RCV_DOC_DETAIL].
[LINE_NO]) MS SQL Query Analyzer WMS_ADMIN
0 2276 15
13:50:00.037
One of the first things I tried last week was running a
backup, statistics and the DB optimizations on the DB
before running EOD - and that seemed to help.
Are those warning above something that may be causing this?
>--Original Message--
>Does your procedure mail out results using SQL Mail or
SQL Agent Mail? If
>so, it is probably stuck on a hung MAPI interface call.
>|||Oh and Order_date is not part of any index..?
I ran DBCC Checktable and reindex - no help.
I read somewhere when doing a google seach that someone
fixed a similar problem by exporting the data, recreating
the tables, and reimporting. I hate to do that because
even if it solves the problem, in my mind it is not
fixed. Do you think that would help?
>+Missing Column Statistics NO STATS:([Rcv_Doc].
>[ORDER_DATE]) MS SQL Query Analyzer WMS_ADMIN
> 0 2276 15
> 13:49:59.770|||Well recreating those table did not help.
I also added an index on order_date just for kicks and it
made the error go away - but same ultimate result - the SP
hanging. I removed the index and the warning came back.
Interestingly enough, the other warning on Line_no for the
other table has not been warning since the tables were
recreated. There IS an index on that col as it is part of
the PK.|||Maybe it is fixed' I went back and did add the WITH
RECOMPILE option and it has now run about 20 times
straight. That is not 100% guranteed indicator which is
what is making this SO frustrating - it has worked after
other changes for a little while.
But I am hoping that fixes it. Have you seen it cause
exactly what I describe if you do not have that option
set? The queries do not use the LIKE clause and this
basic EOD routine has been in production for years..|||There are other issues with SP plan reuse, especially with SQL 7.0. Most of
the time reuse is a good thing. Sometimes it is not, especially with result
sets that vary widely in size. If the WITH RECOMPILE works and solves this
problem, then move on to the next one.
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
"Patrick" <anonymous@.discussions.microsoft.com> wrote in message
news:094001c3b070$97f61b20$a001280a@.phx.gbl...
> Maybe it is fixed' I went back and did add the WITH
> RECOMPILE option and it has now run about 20 times
> straight. That is not 100% guranteed indicator which is
> what is making this SO frustrating - it has worked after
> other changes for a little while.
> But I am hoping that fixes it. Have you seen it cause
> exactly what I describe if you do not have that option
> set? The queries do not use the LIKE clause and this
> basic EOD routine has been in production for years..
>sql

Wednesday, March 21, 2012

Invalid length parameter passed to the SUBSTRING function

Hi all,

I am having a weird issue after we upgraded our DB server to SQL 2005.

I have a SP used to extract exchange rate, and a job calls this SP daily. This job worked fine on SQL 2000, and works very well in Management studio if I call this SP seperately, but failed in sql job in 2005.

The error statement pointed to:

select left(@.row, charindex(',', @.row)-1), REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1))

The error message is:

Invalid length parameter passed to the SUBSTRING function.

Anyone knows what's the difference for LEFT function between sql 2000 and 2005?

Thanks

Bill

I would imagine CHARINDEX is either returning a NULL or a 0 (see below). It may be better storing the resulting of the CHARINDEX in a variable before running the query (if it is the same for all values), or testing the value returned before doing the left. Or use ISNULL if it is returning NULL. I could not find any documentation suggesting differences between the function in 2000 vs 2005. Has your database compatibility level changed?

Clarity Consulting (www.claritycon.com)

http://blogs.claritycon.com/blogs/the_englishman/default.aspx

Clarity Consulting (www.claritycon.com)

CHARINDEX link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_left_7910.asp

CHARINDEX ( expression1 , expression2 [ , start_location ] )

Arguments

expression1

Is an expression containing the sequence of characters to be found. expression1 is an expression of the short character data type category.

expression2

Is an expression, usually a column searched for the specified sequence. expression2 is of the character string data type category.

start_location

Is the character position to start searching for expression1 in expression2. If start_location is not given, is a negative number, or is zero, the search starts at the beginning of expression2.

Return Types

int

Remarks

If either expression1 or expression2 is of a Unicode data type (nvarchar or nchar) and the other is not, the other is converted to a Unicode data type.

If either expression1 or expression2 is NULL, CHARINDEX returns NULL when the database compatibility level is 70 or later. If the database compatibility level is 65 or earlier, CHARINDEX returns NULL only when both expression1 and expression2 are NULL.

If expression1 is not found within expression2, CHARINDEX returns 0.

|||

Hi Shughes,

Thanks for your response.

The problem was not caused by NULL or 0. I set up trace and found that it actually caused by another statement.

-- select @.pos = charindex('United States Dollar', @.sourcedoc)

-- select @.len = len(@.sourcedoc) - @.pos

select @.doc = substring(@.sourcedoc, @.pos, @.len)

-- exec spReportSQLError 'Tracing', @.doc

It seems this statement will generate different result running in SQL job or in management studio.

Here is the trace results:

Error Details:(from SQL Job: wrong data)

-

Error Date: Feb 6 2006 12:43PM

Error Number: 0

Error Severity: 0

Error State: 0

Error Procedure: None

Error Line: 0

Error Message: No details info.

Other Info: # The daily noon exchange rates for major foreign currencies are published every business day at about 1 # p.m. EST. They are obtained from market or official sources around noon, and show the rates for the # various currencies in Canadian dollars converted from US dollars. The rates are nominal quotations - # neither buying nor selling rates - and are intended for statistical or analytical purposes. Rates # available from financial institutions will differ.

#

Date (<m>/<d>/<year>),01/27/2006,01/30/

Error Details(Run in management studio(Correct data)):

Error Date: Feb 6 2006 12:43PM

Error Number: 0

Error Severity: 0

Error State: 0

Error Procedure: None

Error Line: 0

Error Message: No details info.

Other Info: United States Dollar,1.1474,1.1443,1.1439,1.1402,1.1432,1.1471,1.1457

Argentine Peso (Floating Rate),0.3733,0.3729,0.3725,0.3712,0.3716,0.3729,0.3727

Australian Dollar,0.8624,0.8577,0.8660,0.8598,0.8628,0.8591,0.8507

Bahamian Dollar,1.1474,1.1443,1.1439,1.1402,1.1432,1.1471,1.1457

Brazilian Real,0.5201,0.5181,0.5174,0.5138,0.5132,0.5171,0.5242

Chilean Peso,0.002178,0.002183,0.002173,0.002158,0.002156,0.002171,0.002183

Chinese Renminbi,0.1423,0.1419,0.1419,0.1414,0.1418,0.1423,0.1422

Colombian Peso,0.000506,0.000505,0.000504,0.000503,0.000505,0.000507,0.000507

Croatian Kuna,0.1893,0.1882,0.1895,0.1880,0.1886,0.1883,0.1870

Czech. Republic Koruna,0.04920,0.04875,0.04907,0.04843,0.04849,0.04848,0.04844

Danish Krone,0.1865,0.1854,0.1863,0.1847,0.1853,0.1847,0.1836

East Caribbean Dollar,0.4265,0.4253,0.4252,0.4238,0.4249,0.4264,0.4259

European EURO,1.3919,1.3836,1.3906,1.3786,1.3833,1.3786,1.3713

Fiji Dollar,0.6689,0.6676,0.6640,0.6647,0.6643,0.6688,0.6687

African Financial Community Franc (CFA),0.002122,0.002109,0.002120,0.002102,0.002109,0.002102,0.002091

Pacific Financial Community Franc (CFP),0.01166,0.01159,0.01165,0.01155,0.01159,0.01155,0.01149

Ghanaian Cedi,0.000126,0.000126,0.000126,0.000125,0.000125,0.000126,0.000126

Guatemala Quetzal,0.15058,0.15017,0.15036,0.14988,0.15027,0.15079,0.15060

Honduran Lempira,0.06073,0.06056,0.06054,0.06034,0.06050,0.06071,0.06064

Hong Kong Dollar,0.147927,0.147512,0.147461,0.146989,0.147382,0.147865,0.147674

Hungarian Forint,0.005534,0.005495,0.005526,0.005479,0.005512,0.005502,0.005479

Icelandic Krona,0.01851,0.01840,0.01833,0.01806,0.01812,0.01816,0.01816

Indian Rupee,0.02607,0.02598,0.02602,0.02583,0.02588,0.02598,0.02595

Indonesian Rupiah,0.000122,0.000122,0.000122,0.000122,0.000122,0.000123,0.000124

Israeli New Shekel,0.2476,0.2463,0.2450,0.2443,0.2439,0.2441,0.2435

Jamaican dollar,0.01784,0.01780,0.01781,0.01869,0.01874,0.01783,0.01780

Japanese Yen,0.009793,0.009735,0.009785,0.009674,0.009665,0.009643,0.009632

Malaysian Ringgit,0.3059,0.3051,0.3050,0.3040,0.3046,0.3063,0.3064

Mexican Peso,0.1097,0.1096,0.1095,0.1093,0.1090,0.1093,0.1095

Moroccan Dirham,0.1273,0.1267,0.1271,0.1259,0.1265,0.1265,0.1259

Myanmar (Burma) Kyat,0.1954,0.1942,0.1943,0.1937,0.1937,0.1944,0.1934

Neth. Antilles Guilder,0.6446,0.6429,0.6426,0.6406,0.6422,0.6444,0.6437

New Zealand Dollar,0.7837,0.7802,0.7840,0.7816,0.7879,0.7878,0.7805

Norwegian Krona,0.1722,0.1700,0.1719,0.1707,0.1721,0.1714,0.1704

Pakistan Rupee,0.01917,0.01907,0.01911,0.01904,0.01909,0.01917,0.01914

Panamanian Balboa,1.1474,1.1443,1.1439,1.1402,1.1432,1.1471,1.1457

Peruvian New Sol,0.3460,0.3458,0.3450,0.3449,0.3452,0.3479,0.3484

Philippine Peso,0.02187,0.02181,0.02193,0.02190,0.02196,0.02208,0.02214

Polish Zloty,0.3641,0.3623,0.3641,0.3609,0.3615,0.3605,0.3590

Russian Rouble,0.04098,0.04066,0.04068,0.04051,0.04063,0.04062,0.04056

Singapore Dollar,0.7060,0.7019,0.7050,0.6998,0.7001,0.7015,0.7038

Slovak Koruna,0.03725,0.03706,0.03721,0.03696,0.03702,0.03693,0.03671

Slovenian Tolar,0.005809,0.005778,0.005805,0.005754,0.005775,0.005760,0.005727

South African Rand,0.1867,0.1862,0.1879,0.1864,0.1878,0.1880,0.1874

South Korean Won,0.001182,0.001179,0.001190,0.001185,0.001176,0.001182,0.001190

Sri Lanka Rupee,0.01123,0.01120,0.01120,0.01117,0.01119,0.01123,0.01123

Swedish Krona,0.1507,0.1498,0.1504,0.1491,0.1489,0.1487,0.1474

Swiss Franc,0.8967,0.8894,0.8945,0.8878,0.8900,0.8861,0.8806

Taiwanese New Dollar,0.03588,0.03578,0.03577,0.03565,0.03575,0.03574,0.03570

Thai Baht,0.02941,0.02926,0.02940,0.02904,0.02903,0.02910,0.02906

Trinidad & Tobago Dollar,0.1833,0.1831,0.1845,0.1823,0.1823,0.1840,0.1835

Tunisian Dinar,0.8578,0.8524,0.8570,0.8488,0.8510,0.8500,0.8468

New Turkish Lira,0.8647,0.8643,0.8651,0.8612,0.8634,0.8664,0.8624

Pound Sterling,2.0342,2.0240,2.0377,2.0269,2.0357,2.0213,2.0006

Venezuelan Bolivar,0.000534,0.000533,0.000533,0.000531,0.000532,0.000534,0.000534

I am still trying to figure out how this was caused.

Thanks

Bill

|||? Hi Bill, It's hard to know without knowing the complete code and the data it operates on. But I suspect that this was a potential bug in your code all along, and you just were lucky until now. The SQL Server query optimizer is free to reorganize your query and evaulate it in any order it wants. That can give you great performance benefits - but it may also cause unexpected bugs. I'll illustrate this with the example you posted in your first post (yes, I did see the follow-up, but this code takes a lot less typing <g>). Have a look at this query: SELECT LEFT(MyColumn, CHARINDEX(',', MyColumn) - 1) FROM MyTable WHERE CHARINDEX(',', MyColumn) > 0 You might be inclined to say that this is safe - after all, the WHERE will exclude all rows without a comma, and the LEFT function will evaluate fine for the remaining rows. Right? WRONG!!!! The optimizer is free to evaluate the query in any order it sees fit. So it might decide to do the SELECT first, then use the WHERE to filter the results. And BOOM!! you get an error for the first row with no comma in MyColumn. What might have happened is that in your real query, which is probably more complex than the example above, a new optimizing technique (that was not available to the SQL Server 2000 query optimizer) was chosen to evaluate your query, leading to this result. -- Hugo Kornelis, SQL Server MVP <Bill YU@.discussions.microsoft.com> schreef in bericht news:7d07a949-c4d2-4fa9-af97-bad706de680d@.discussions.microsoft.com... Hi all, I am having a weird issue after we upgraded our DB server to SQL 2005. I have a SP used to extract exchange rate, and a job calls this SP daily. This job worked fine on SQL 2000, and works very well in Management studio if I call this SP seperately, but failed in sql job in 2005. The error statement pointed to: select left(@.row, charindex(',', @.row)-1), REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1)) The error message is: Invalid length parameter passed to the SUBSTRING function. Anyone knows what's the difference for LEFT function between sql 2000 and 2005? Thanks Bill|||

Hi Hugo,

Here are my 3 SPs:

CREATE PROCEDURE uspGetXMLFromHTTP (@.URL varchar(255), @.Method varchar(20)='GET')
AS
BEGIN
set nocount on
declare @.objRef int,@.resultcode int
exec @.resultcode = sp_OACreate 'Msxml2.XMLHTTP.4.0', @.objRef OUT
if @.resultcode = 0
begin
exec @.resultcode = sp_OAMethod @.objRef, 'Open', NULL,@.Method, @.URL, False
exec @.resultcode = sp_OAMethod @.objRef, 'Send',null
execute sp_OAGetProperty @.objRef, 'responseText'
end
exec sp_OADestroy @.objRef
END
GO

CREATE PROCEDURE uspReportSQLError(@.Location Varchar(250) = null, @.TraceInfo varchar(MAX) = null)
AS
BEGIN
set nocount on
declare @.cc varchar(250), @.bcc varchar(250)
declare @.errormsg varchar(Max), @.subject varchar(250)
select @.cc = '',
@.bcc = '',
@.subject = 'SQL Error: Server > [' + @.@.servername + '] > Database > [' + DB_NAME() + ']'
+ isnull((' > Location > [' + isnull(@.Location, ERROR_PROCEDURE()) + ']'), '')

select @.errormsg = 'Error Details:' + char(13) + char(10)
+ '-' + char(13) + char(10)
+ ' Error Date: ' + cast(getdate() as varchar) + char(13) + char(10)
+ ' Error Number: ' + cast(isnull(ERROR_NUMBER(), 0) as varchar) + char(13) + char(10)
+ ' Error Severity: ' + cast(isnull(ERROR_SEVERITY(), 0) as varchar) + char(13) + char(10)
+ ' Error State: ' + cast(isnull(ERROR_STATE(), 0) as varchar) + char(13) + char(10)
+ 'Error Procedure: ' + isnull(ERROR_PROCEDURE(), 'None') + char(13) + char(10)
+ ' Error Line: ' + cast(isnull(ERROR_LINE(), 0) as varchar) + char(13) + char(10)
+ ' Error Message: ' + isnull(ERROR_MESSAGE(), 'No details info.') + char(13) + char(10) + char(13) + char(10)
+ isnull((' Other Info: ' + @.TraceInfo), '')
-- Insert central log table

exec msdb.dbo.sp_send_dbmail @.profile_name = 'SQLError',
@.recipients = 'dba@.builddirect.com',
@.copy_recipients = @.cc,
@.blind_copy_recipients = @.bcc,
@.body = @.errormsg,
@.subject = @.subject,
@.importance = 'High',
@.body_format = 'Text'
set nocount off
END
GO

CREATE PROCEDURE uspExtractExchangeRate
AS
BEGIN
set nocount on
declare @.currency varchar(100), @.rate decimal(12,6), @.pos int, @.len int
declare @.doc varchar(Max), @.row varchar(255), @.reversedrow varchar(255), @.i int, @.j int
declare @.table table(Currency varchar(100), Rate decimal(12,6))
create table #TodayExchangeRate(response varchar(MAX))
begin try
insert #TodayExchangeRate
exec uspGetXMLFromHTTP 'http://www.bankofcanada.ca/en/financial_markets/csv/exchange_eng.csv'
select @.doc = response from #TodayExchangeRate
drop table #TodayExchangeRate

select @.pos = charindex('United States Dollar', @.doc)
select @.len = len(@.doc) - @.pos
select @.doc = substring(@.doc, @.pos, @.len)

--exec uspReportSQLError 'Tracing', @.doc

select @.i = 1, @.j = -1
while (1=1)
begin
select @.j = charindex((char(13) + char(10)), @.doc, @.j + 2)
if @.j = 0 break
select @.row = substring(@.doc, @.i, @.j - @.i)
select @.reversedrow = REVERSE(@.row)
select @.currency = left(@.row, charindex(',', @.row)-1)
select @.rate = REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1))
insert @.table(Currency, Rate)
select @.currency, @.rate
select @.i = @.j + 2
end
end try
begin catch
--exec uspReportSQLError
end catch

-- save into exchangeRates table
set nocount off
END
GO

Except resetting @.profile_name in [uspExtractExchangeRate], these SPs are functional.

Hope this help.

Thanks

Bill


|||? Hi Bill, This'll be hard to troubleshoot, since I have no idea what xp_OACreate 'Msxml2.XMLHTTP.4.0' does. And I don't have SQL Server 2005, so I can't run this. However, I do have a trouble-shooting suggestion: add some PRINT (or SELECT) statements to your main procedure to see what happens, what exact data is being returned from the calls to sp_OAMethod and what steps are taken during the string parsing process. Something like this: CREATE PROCEDURE uspExtractExchangeRateASBEGINset nocount ondeclare @.currency varchar(100), @.rate decimal(12,6), @.pos int, @.len intdeclare @.doc varchar(8000), @.row varchar(255), @.reversedrow varchar(255), @.i int, @.j intdeclare @.table table(Currency varchar(100), Rate decimal(12,6))create table #TodayExchangeRate(response varchar(8000))begin tryinsert #TodayExchangeRateexec uspGetXMLFromHTTP 'http://www.bankofcanada.ca/en/financial_markets/csv/exchange_eng.csv' select @.doc = response from #TodayExchangeRate SELECT @.doc AS 'After uspGetXMLFromHTTP'drop table #TodayExchangeRate select @.pos = charindex('United States Dollar', @.doc)select @.len = len(@.doc) - @.posselect @.doc = substring(@.doc, @.pos, @.len)SELECT @.doc AS 'After stripping'--exec uspReportSQLError 'Tracing', @.doc select @.i = 1, @.j = -1while (1=1)beginselect @.j = charindex((char(13) + char(10)), @.doc, @.j + 2)if @.j = 0 breakselect @.row = substring(@.doc, @.i, @.j - @.i)select @.reversedrow = REVERSE(@.row) SELECT @.i, @.j, @.row, @.reversedrow select @.currency = left(@.row, charindex(',', @.row)-1)select @.rate = REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1)) SELECT @.currency, @.rateinsert @.table(Currency, Rate)select @.currency, @.rateselect @.i = @.j + 2endSELECT @.i, @.j, 'Completely done' end trybegin catch--exec uspReportSQLErrorend catch -- save into exchangeRates tableset nocount offENDGO Checking the output of this debug-enabled version of the proc might reveal what's going on. -- Hugo Kornelis, SQL Server MVP -- Original Message -- From: Bill YU@.discussions.microsoft.com Newsgroups: microsoft.private.forums.msdn.sqlserver.tsql Sent: Monday, February 06, 2006 11:59 PM Subject: Re: Invalid length parameter passed to the SUBSTRING function Hi Hugo, Here are my 3 SPs: CREATE PROCEDURE uspGetXMLFromHTTP (@.URL varchar(255), @.Method varchar(20)='GET')ASBEGINset nocount ondeclare @.objRef int,@.resultcode intexec @.resultcode = sp_OACreate 'Msxml2.XMLHTTP.4.0', @.objRef OUT if @.resultcode = 0beginexec @.resultcode = sp_OAMethod @.objRef, 'Open', NULL,@.Method, @.URL, False exec @.resultcode = sp_OAMethod @.objRef, 'Send',nullexecute sp_OAGetProperty @.objRef, 'responseText'endexec sp_OADestroy @.objRefENDGO CREATE PROCEDURE uspReportSQLError(@.Location Varchar(250) = null, @.TraceInfo varchar(MAX) = null)ASBEGINset nocount ondeclare @.cc varchar(250), @.bcc varchar(250)declare @.errormsg varchar(Max), @.subject varchar(250)select @.cc = '',@.bcc = '',@.subject = 'SQL Error: Server > [' + @.@.servername + '] > Database > [' + DB_NAME() + ']'+ isnull((' > Location > [' + isnull(@.Location, ERROR_PROCEDURE()) + ']'), '')select @.errormsg = 'Error Details:' + char(13) + char(10)+ '-' + char(13) + char(10)+ ' Error Date: ' + cast(getdate() as varchar) + char(13) + char(10) + ' Error Number: ' + cast(isnull(ERROR_NUMBER(), 0) as varchar) + char(13) + char(10)+ ' Error Severity: ' + cast(isnull(ERROR_SEVERITY(), 0) as varchar) + char(13) + char(10)+ ' Error State: ' + cast(isnull(ERROR_STATE(), 0) as varchar) + char(13) + char(10)+ 'Error Procedure: ' + isnull(ERROR_PROCEDURE(), 'None') + char(13) + char(10)+ ' Error Line: ' + cast(isnull(ERROR_LINE(), 0) as varchar) + char(13) + char(10)+ ' Error Message: ' + isnull(ERROR_MESSAGE(), 'No details info.') + char(13) + char(10) + char(13) + char(10)+ isnull((' Other Info: ' + @.TraceInfo), '')-- Insert central log table exec msdb.dbo.sp_send_dbmail @.profile_name = 'SQLError',@.recipients = 'dba@.builddirect.com',@.copy_recipients = @.cc,@.blind_copy_recipients = @.bcc,@.body = @.errormsg,@.subject = @.subject,@.importance = 'High',@.body_format = 'Text' set nocount offENDGO CREATE PROCEDURE uspExtractExchangeRateASBEGINset nocount ondeclare @.currency varchar(100), @.rate decimal(12,6), @.pos int, @.len intdeclare @.doc varchar(Max), @.row varchar(255), @.reversedrow varchar(255), @.i int, @.j intdeclare @.table table(Currency varchar(100), Rate decimal(12,6))create table #TodayExchangeRate(response varchar(MAX))begin tryinsert #TodayExchangeRateexec uspGetXMLFromHTTP 'http://www.bankofcanada.ca/en/financial_markets/csv/exchange_eng.csv' select @.doc = response from #TodayExchangeRatedrop table #TodayExchangeRateselect @.pos = charindex('United States Dollar', @.doc)select @.len = len(@.doc) - @.posselect @.doc = substring(@.doc, @.pos, @.len) --exec uspReportSQLError 'Tracing', @.doc select @.i = 1, @.j = -1while (1=1)beginselect @.j = charindex((char(13) + char(10)), @.doc, @.j + 2)if @.j = 0 breakselect @.row = substring(@.doc, @.i, @.j - @.i)select @.reversedrow = REVERSE(@.row) select @.currency = left(@.row, charindex(',', @.row)-1)select @.rate = REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1))insert @.table(Currency, Rate)select @.currency, @.rateselect @.i = @.j + 2endend trybegin catch--exec uspReportSQLErrorend catch -- save into exchangeRates tableset nocount offENDGO Except resetting @.profile_name in [uspExtractExchangeRate], these SPs are functional. Hope this help. Thanks Bill <Bill YU@.discussions.microsoft.com> schreef in bericht news:3285cccb-7a6a-4451-b618-a8d90cdf19c1@.discussions.microsoft.com... Hi Hugo, Here are my 3 SPs: CREATE PROCEDURE uspGetXMLFromHTTP (@.URL varchar(255), @.Method varchar(20)='GET')ASBEGINset nocount ondeclare @.objRef int,@.resultcode intexec @.resultcode = sp_OACreate 'Msxml2.XMLHTTP.4.0', @.objRef OUT if @.resultcode = 0beginexec @.resultcode = sp_OAMethod @.objRef, 'Open', NULL,@.Method, @.URL, False exec @.resultcode = sp_OAMethod @.objRef, 'Send',nullexecute sp_OAGetProperty @.objRef, 'responseText'endexec sp_OADestroy @.objRefENDGO CREATE PROCEDURE uspReportSQLError(@.Location Varchar(250) = null, @.TraceInfo varchar(MAX) = null)ASBEGINset nocount ondeclare @.cc varchar(250), @.bcc varchar(250)declare @.errormsg varchar(Max), @.subject varchar(250)select @.cc = '',@.bcc = '',@.subject = 'SQL Error: Server > [' + @.@.servername + '] > Database > [' + DB_NAME() + ']'+ isnull((' > Location > [' + isnull(@.Location, ERROR_PROCEDURE()) + ']'), '')select @.errormsg = 'Error Details:' + char(13) + char(10)+ '-' + char(13) + char(10)+ ' Error Date: ' + cast(getdate() as varchar) + char(13) + char(10) + ' Error Number: ' + cast(isnull(ERROR_NUMBER(), 0) as varchar) + char(13) + char(10)+ ' Error Severity: ' + cast(isnull(ERROR_SEVERITY(), 0) as varchar) + char(13) + char(10)+ ' Error State: ' + cast(isnull(ERROR_STATE(), 0) as varchar) + char(13) + char(10)+ 'Error Procedure: ' + isnull(ERROR_PROCEDURE(), 'None') + char(13) + char(10)+ ' Error Line: ' + cast(isnull(ERROR_LINE(), 0) as varchar) + char(13) + char(10)+ ' Error Message: ' + isnull(ERROR_MESSAGE(), 'No details info.') + char(13) + char(10) + char(13) + char(10)+ isnull((' Other Info: ' + @.TraceInfo), '')-- Insert central log table exec msdb.dbo.sp_send_dbmail @.profile_name = 'SQLError',@.recipients = 'dba@.builddirect.com',@.copy_recipients = @.cc,@.blind_copy_recipients = @.bcc,@.body = @.errormsg,@.subject = @.subject,@.importance = 'High',@.body_format = 'Text' set nocount offENDGO CREATE PROCEDURE uspExtractExchangeRateASBEGINset nocount ondeclare @.currency varchar(100), @.rate decimal(12,6), @.pos int, @.len intdeclare @.doc varchar(Max), @.row varchar(255), @.reversedrow varchar(255), @.i int, @.j intdeclare @.table table(Currency varchar(100), Rate decimal(12,6))create table #TodayExchangeRate(response varchar(MAX))begin tryinsert #TodayExchangeRateexec uspGetXMLFromHTTP 'http://www.bankofcanada.ca/en/financial_markets/csv/exchange_eng.csv' select @.doc = response from #TodayExchangeRatedrop table #TodayExchangeRateselect @.pos = charindex('United States Dollar', @.doc)select @.len = len(@.doc) - @.posselect @.doc = substring(@.doc, @.pos, @.len) --exec uspReportSQLError 'Tracing', @.doc select @.i = 1, @.j = -1while (1=1)beginselect @.j = charindex((char(13) + char(10)), @.doc, @.j + 2)if @.j = 0 breakselect @.row = substring(@.doc, @.i, @.j - @.i)select @.reversedrow = REVERSE(@.row) select @.currency = left(@.row, charindex(',', @.row)-1)select @.rate = REVERSE(left(@.reversedrow, charindex(',', @.reversedrow)-1))insert @.table(Currency, Rate)select @.currency, @.rateselect @.i = @.j + 2endend trybegin catch--exec uspReportSQLErrorend catch -- save into exchangeRates tableset nocount offENDGO Except resetting @.profile_name in [uspExtractExchangeRate], these SPs are functional. Hope this help. Thanks Bill|||

Why are you using SQL to parse the comma-separated string? This is much more easier to do on the client. You can do any of the following:

1. You can replace the SP with a DTS or SSIS package that imports the CSV information to a table

2. Or you can take the CSV file and import using BCP

3. Or use an ActiveX task from SQLAgent

Any of these approaches will be much more robust and simpler. OLE automation SPs should generally be avoided due to their overhead in using on the server and since you are running this from a job anyway it is better to isolate the process from server.

Btw, the error is probably due to bad data or incorrectly formed row.

|||

Thanks Hugo.

Bill

|||

Hi Umachandar,

This is a legacy task, I run this job from Internal DB server not production, and I did not want to develop and maintain any codes other than sql scripts.

I just happened to have this problem, seems SQL 2005 has a different behavior here.

Thanks

Bill

|||You will have to post a repro script to determine if this is a bug in SQL Server 2005 or not. Otherwise it is hard to tell by just looking at the code since this can be due to bad input. Does the same code run fine in SQL Server 2000 with the same input?|||

Yes,

I run this job for more than two years, never had a problem.

The issue happened after we upgraded to SQL 2005.

Interestingly, if I assign the CSV text to a variable from TEMP table, it works fine.

Now, I have to manually execute the same SP from Management Studio daily.

Bill

Friday, February 24, 2012

Interview question

Hi,
I had a job interview yesterday and they gave me a small test to complete.
One of the questions was the following... I was not sure what to answer...
If you have the following table:
CREATE TABLE [Customers] (
[FirstName] [varchar] (50) NOT NULL ,
[LastName] [varchar] (50) NOT NULL
) ON [PRIMARY]
And all the queries that you will have for this table are like these:
1. LastName ='Simpson'
2. FirstName ='John' and LastName='Smith'
3. LastName='Parker' and FirstName like 'J%'
4. LastName ='Owen'
5. FirstName ='Danny' and LastName='Jackson'
6. LastName='Owen' and FirstName= 'Michael'
7. LastName like 'A%'
.
(with different values for FirstName and LastName):
What kind of index (only one) do you think it would be more effective for
running those queries faster? Please, explain why.
Any ideas?
Thanks!!On Thu, 10 Mar 2005 10:07:23 -0500, Star wrote:
(snip)
>What kind of index (only one) do you think it would be more effective for
>running those queries faster? Please, explain why.
Hi Star,
A composite index on (lastname, firstname) would be best. Since there
are no other columns in this table, might as well make it clustered.
Reason: all queries presented include at least the last name (or the
start of the last name); some include (part of) the first name as well,
so each query can use this index to immediately jump to the part of the
index where matches are.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Clustered index on (lastname, firstname).
The common column in all queries is Lastname so it should appear first in
the index. Clustered because if you have only one index it usually makes
sense to cluster it.
David Portas
SQL Server MVP
--|||create nonclustered index ix_nc_customers_lastname_firstname on
customers(lastname, firstname)
go
- nonclustered
- composite
- first column should be LastName in order to allow 1, 4 and 7
AMB
"Star" wrote:

> Hi,
> I had a job interview yesterday and they gave me a small test to complete.
> One of the questions was the following... I was not sure what to answer...
> If you have the following table:
> CREATE TABLE [Customers] (
> [FirstName] [varchar] (50) NOT NULL ,
> [LastName] [varchar] (50) NOT NULL
> ) ON [PRIMARY]
> And all the queries that you will have for this table are like these:
> 1. LastName ='Simpson'
> 2. FirstName ='John' and LastName='Smith'
> 3. LastName='Parker' and FirstName like 'J%'
> 4. LastName ='Owen'
> 5. FirstName ='Danny' and LastName='Jackson'
> 6. LastName='Owen' and FirstName= 'Michael'
> 7. LastName like 'A%'
> ..
> (with different values for FirstName and LastName):
>
> What kind of index (only one) do you think it would be more effective for
> running those queries faster? Please, explain why.
>
> Any ideas?
> Thanks!!
>
>|||An Index on LastName, FirstName would be the most optimal as there is no
occurance of
FirstName column only.
The above index will be used by both the criterias which have
LastName,FirstName and just
LastName.
Gopi
"Star" <nospam@.nospam.com> wrote in message
news:e88xfLYJFHA.1396@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I had a job interview yesterday and they gave me a small test to complete.
> One of the questions was the following... I was not sure what to answer...
> If you have the following table:
> CREATE TABLE [Customers] (
> [FirstName] [varchar] (50) NOT NULL ,
> [LastName] [varchar] (50) NOT NULL
> ) ON [PRIMARY]
> And all the queries that you will have for this table are like these:
> 1. LastName ='Simpson'
> 2. FirstName ='John' and LastName='Smith'
> 3. LastName='Parker' and FirstName like 'J%'
> 4. LastName ='Owen'
> 5. FirstName ='Danny' and LastName='Jackson'
> 6. LastName='Owen' and FirstName= 'Michael'
> 7. LastName like 'A%'
> .
> (with different values for FirstName and LastName):
>
> What kind of index (only one) do you think it would be more effective for
> running those queries faster? Please, explain why.
>
> Any ideas?
> Thanks!!
>|||Thank you, guys!
It helped a lot... now I know for the next time :(|||this interview wasn't in Orlando (or Celebration) Florida was it ?
Greg Jackson
PDX, Oregon|||definately Clustered on LastName, FirstName
GAJ

interrupt backup job

Hi,
If you stop a db full backup job in the middle of process,
will this mess up your db or server?
working on sql server 2000.
thanks!!
JJHi,
Dont worry, Nothing will happen.
Thanks
Hari
MCDBA
"JJ Wang" <anonymous@.discussions.microsoft.com> wrote in message
news:04a901c3ab35$bb0be080$a501280a@.phx.gbl...
> Hi,
> If you stop a db full backup job in the middle of process,
> will this mess up your db or server?
> working on sql server 2000.
> thanks!!
> JJ

Sunday, February 19, 2012

Interpret Agent Log Entry

The log entry
2006-09-19 21:16:06 - + [000] Request to run job
0x6A98EE728DB2FC498C379735F3CE7567 (from Alert 11) refused
appears in our SQL Server Agent log. I'm try to track the source. So I'm
wondering what the hex value refer to and the bit "(from Alert 11)".
Thanks,
TomHard to say, could you query select * from msdb.dbo.sysjobs where
jobid=0x6A98EE728DB2FC498C379735F3CE7567
to determine which job this id belongs to
--
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"T Morris" <TMorris@.discussions.microsoft.com> wrote in message
news:40AB8EB6-A0F7-40BF-93D3-ABBBB73F9706@.microsoft.com...
> The log entry
> 2006-09-19 21:16:06 - + [000] Request to run job
> 0x6A98EE728DB2FC498C379735F3CE7567 (from Alert 11) refused
> appears in our SQL Server Agent log. I'm try to track the source. So I'm
> wondering what the hex value refer to and the bit "(from Alert 11)".
> Thanks,
> Tom