0

I'm no expert in SQL Server and this question may be a little convoluted, but I have to try something!

I'm running through each database on a server in order to gather some stats. In development of the correct SQL code, I was using a relatively small server (~150 databases). I get the result that the query has executed with errors, and this is fine - it's expected. All the stuff that needs to be done gets done and I get my results.

Now my problem is when I use the live server. This is granted, a little bigger with much more data. I don't mind my query taking a long time to run, my problem is that when it does run, eventually it stops (it could stop after 20 databases have been done, it could stop after 30 - it's never the same). It has the same Query executed with errors thing, but as I mentioned, this is fine - I've worked perfectly well with this error before (it's a table that's not in every database). So basically, the query, though it is not finished, stops anyway.

I'm just wondering, does anyone know what may be wrong? I'll post my query but I'm not sure if this is the problem, seeing as it's working fine on the dev server..

I'm tearing my hair out trying to figure this out - Google ain't being friendly today either. As I said, I'm no expert - I'm relatively new to real-life development so I could be doing something naive.

This is just a query run in SQL Server - it's not part of a bigger project, it stands alone.

declare @name nvarchar(100)
declare @sql varchar(2000)
declare @sql2 varchar (2000)
declare @repname nvarchar (200), @time DateTime, @pos int
declare @rn nvarchar (200), @ts DateTime, @p int

DECLARE GetDatabaseName CURSOR LOCAL FOR
    SELECT Name FROM sys.sysdatabases WHERE name not in ('master', 'tempdb', etc') order by name

OPEN GetDatabaseName
FETCH NEXT FROM GetDatabaseName INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON
print @name
DROP table Results
DROP table TrueResults
DROP table temp
Create table Results (Position int, Name nvarchar(200), TimeStamp DateTime)
Create table TrueResults (Position int, Name nvarchar (200), TimeStamp DateTime)
Create table temp (Position int, Name nvarchar (200), TimeStamp DateTime)

ALTER TABLE Results
ALTER COLUMN Name VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
set @sql2 = 'insert into Results select ROW_NUMBER()over (order by TimeStamp), Name, TimeStamp from ' + @name                 + '.dbo.MyTableName'
exec (@sql2)
Declare Curses CURSOR
FOR select Name, TimeStamp, Position from Results
    Open Curses
while @@FETCH_STATUS = 0
    BEGIN
        fetch next from Curses
        into @rname, @time, @pos
        if exists (select * from Results where @rname= 'What I'm Looking For')
        BEGIN       
            if exists (select * from Results where Position = @pos + 1 and Name != 'What I'm Looking                For')
            BEGIN
                insert into temp (Position, Name, TimeStamp)
                values (@pos, @rname, @time)
                set @pos = (select temp.Position from temp, Results where temp.Position =                       Results.Position and Results.Position = @pos)+1
                set @p = (select Position from Results where Position = @pos)
                set @rn = (select Name from Results where Position = @pos)
                set @ts = (select TimeStamp from Results where Position = @pos)
                insert into TrueResults (Position, Name, TimeStamp)
                values (@p, @rn, @ts)
            END
        END
    END
CLOSE Curses
DEALLOCATE Curses

select @name
 set @sql = 'select Name, count(Name) as RCount from TrueResults group by Name order by RCount desc'
    exec (@sql)
FETCH NEXT FROM GetDatabaseName INTO @name

END
CLOSE GetDatabaseName
DEALLOCATE GetDatabaseName
GO

I've tried the things mentioned here to no avail. I did however add in a clause to only return results from the last 7 days - this made the query run for longer. Changed it to 1 day and it ran the whole way through. (The table has a timestamp attribute). So now I'm wondering if it's to do with the sheer volume of data that's being returned? I wasn't aware that there was any limit, as such. Could something like that cause a problem like this, though? Could it be a memory issue?

  • Are you saying that you are getting the error in **both** environments, but in Dev it continues after the error and in Prod it stops right after? If so, are you using the same tools to run your SQL script in both environments? – PM 77-1 Sep 09 '13 at 17:30
  • 2
    You might want to check the "Remote query timeout" property on your production server. By default it is set to 10 minutes. Depending on the load on your server at the time your query is run that might account for why you see a variance of the number of databases it gets through before ending. You can access the timeout property through the SQL Enterprise Manager GUI, select server, right-click, select properties, and click Connections on the Server Properties dialog. –  Sep 09 '13 at 17:45
  • And make sure that SET XACT_ABORT is the same in both. – RBarryYoung Sep 09 '13 at 18:10
  • @PM77-1 I'm using the exact same tools on both, afaik. I'll double check this, though. – user1261648 Sep 10 '13 at 17:48

1 Answers1

2

firs of all edit this block of query

Declare Curses CURSOR
FOR select Name, TimeStamp, Position from Results
Open Curses
fetch next from Curses
    into @rname, @time, @pos
while @@FETCH_STATUS = 0
BEGIN
    if exists (select * from Results where @rname= 'What Im Looking For')
    BEGIN       
        if exists (select * from Results where Position = @pos + 1 and Name != 'What I'm Looking                For')
        BEGIN
            insert into temp (Position, Name, TimeStamp)
            values (@pos, @rname, @time)
            set @pos = (select temp.Position from temp, Results where temp.Position =                       Results.Position and Results.Position = @pos)+1
            set @p = (select Position from Results where Position = @pos)
            set @rn = (select Name from Results where Position = @pos)
            set @ts = (select TimeStamp from Results where Position = @pos)
            insert into TrueResults (Position, Name, TimeStamp)
            values (@p, @rn, @ts)
        END
    END
    fetch next from Curses
    into @rname, @time, @pos
END
CLOSE Curses
DEALLOCATE Curses

then increase Remote query timeout

  • Can I ask why I need to edit this? It's done that way as that's the only way I knew of to get it to work.. Can you provide me with a better way if this is not the best way, please? I will try the remote timeout thing though, thanks. – user1261648 Sep 10 '13 at 17:47