Avatar billede tuctoh Nybegynder
27. april 2003 - 10:15 Der er 12 kommentarer og
1 løsning

TSQL - at loope igennem et recordset!?

Hejsa,

Jeg skal i et job i min MS SQL database sætte den til først at hente en tabel ved hjælp af en SELECT sætning, derefter skal dataene behandles.
Mit problem er dog hvordan jeg looper igennem et recordset!?

Er der nogen der kan koden for det?

MVH
~Casper Bang
Avatar billede terry Ekspert
27. april 2003 - 10:28 #1
you should be able to find help on this in Books Online
Serach after recordset!
I found this

Using an ADO Connection and Recordset to Check Records
The following ActiveX script creates a connection to the Northwind database and the employee table and counts the number of employee records. If employee records are found in the table, the script displays the number of employees and sends a success flag back to the package. Otherwise, the script sends a failure flag. Those flags can be used to trigger other tasks. For example, the success flag can signal that the table has records and then execute a Bulk Insert task. You can use the failure flag to execute a Send Mail task informing a database administrator (DBA) that a potential problem exists.

dim myConn
dim myRecordset
dim iRowCount

' instantiate the ADO objects
set myConn = CreateObject("ADODB.Connection")
set myRecordset = CreateObject("ADODB.Recordset")

' set the connection properties to point to the Northwind database,
' using the Customers table
myConn.Open = "Provider=SQLOLEDB.1;Data Source=(local); _
  Initial Catalog=Northwind;user id = 'sa';password=''"

mySQLCmdText = "Select 'rowcount' = Count(*) from Customers"

myRecordset.Open mySQLCmdText, myConn

set Flds = myRecordset.Fields
set iRowCount = Flds("rowcount")

If iRowCount.Value = 0 then
  Main = DTSTaskExecResult_Failure
Else
  MsgBox "The number of customers is: " & iRowCount.Value
  Main = DTSTaskExecResult_Success
End If
Avatar billede tuctoh Nybegynder
27. april 2003 - 10:31 #2
øhh... det er i ASP
jeg vil gøre det i TSQL
Avatar billede terry Ekspert
27. april 2003 - 10:37 #3
This is NOT in ASP!
This is taken from MS SQL books online. You can NOT loop through a recordset in SQL!
Avatar billede terry Ekspert
27. april 2003 - 10:38 #4
Try and explain what EXACTLY you are trying to do, that way we can maybe make other suggestions!
Avatar billede tuctoh Nybegynder
27. april 2003 - 10:43 #5
du forstå dansk, så hvorfor ikke svare i det?

Jeg laver en SELECT i TSQL, og vil loope igennem den...
som du beskrev ovenfor, men direkte i TSQL (stored procedure)
Avatar billede arne_v Ekspert
27. april 2003 - 11:05 #6
terry>

Det ligner absolut ikke Transaction SQL.

tuctoh>

Du skal vel erklære en cursor og fetche.
Avatar billede arne_v Ekspert
27. april 2003 - 11:06 #7
DECLARE CURSOR (online books SQLServer 7.0) har følgende eksempel:

SET NOCOUNT ON

 

DECLARE @au_id varchar(11), @au_fname varchar(20), @au_lname varchar(40),

    @message varchar(80), @title varchar(80)

 

PRINT "-------- Utah Authors report --------"

 

DECLARE authors_cursor CURSOR FOR

SELECT au_id, au_fname, au_lname

FROM authors

WHERE state = "UT"

ORDER BY au_id

 

OPEN authors_cursor

 

FETCH NEXT FROM authors_cursor

INTO @au_id, @au_fname, @au_lname

 

WHILE @@FETCH_STATUS = 0

BEGIN

    PRINT " "

    SELECT @message = "----- Books by Author: " +

        @au_fname + " " + @au_lname

 

    PRINT @message

 

    -- Declare an inner cursor based 

    -- on au_id from the outer cursor.

 

    DECLARE titles_cursor CURSOR FOR

    SELECT t.title

    FROM titleauthor ta, titles t

    WHERE ta.title_id = t.title_id AND

    ta.au_id = @au_id    -- Variable value from the outer cursor

 

    OPEN titles_cursor

    FETCH NEXT FROM titles_cursor INTO @title

 

    IF @@FETCH_STATUS <> 0

        PRINT "        <<No Books>>"   

 

    WHILE @@FETCH_STATUS = 0

    BEGIN

       

        SELECT @message = "        " + @title

        PRINT @message

        FETCH NEXT FROM titles_cursor INTO @title

   

    END

 

    CLOSE titles_cursor

    DEALLOCATE titles_cursor

   

    -- Get the next author.

    FETCH NEXT FROM authors_cursor

    INTO @au_id, @au_fname, @au_lname

END

 

CLOSE authors_cursor

DEALLOCATE authors_cursor

GO

 

-------- Utah Authors report --------



----- Books by Author: Anne Ringer

        The Gourmet Microwave

        Is Anger the Enemy?



----- Books by Author: Albert Ringer

        Is Anger the Enemy?

        Life Without
Avatar billede terry Ekspert
27. april 2003 - 11:21 #8
arne>I didnt say it was TSQL, but it is NOT ASP either :o)
Avatar billede tuctoh Nybegynder
27. april 2003 - 11:30 #9
arne, eksemplet er perfekt :)
Tak!

Terry, det du gav var ganske normalt ASP. ASP i vbscript, som er det mest normale ASP.
Avatar billede terry Ekspert
27. april 2003 - 11:35 #10
tuctoh>It  VB in SQL, not ASP, althoughit may look like it!
Avatar billede tuctoh Nybegynder
27. april 2003 - 11:42 #11
ehh, det er rent faktisk vb i ASP...
ja, du connecter til en SQL database med det, men det der kører du fra dine ASP sider...

Ok, i dit job kan det godt være at man i VB-delen ville kunne afvikle dette, det kan jeg så ikke udelukke... Hvis det er det, kunne jeg måske have brugt dette i stedet :s
Men jeg ledte nu efter det i TSQL :)
Avatar billede terry Ekspert
27. april 2003 - 11:45 #12
no problem tuctih, you got the answer you were after in the end :o)
Avatar billede terry Ekspert
27. april 2003 - 11:45 #13
tuctih = tuctoh
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester