2016-12-29 2 views
0

ich Probleme mit dem folgenden Code habe:Erste mehr Spalten in Variablen ausgewählt

/* Cursor */ 
DECLARE @RelationCursor CURSOR 
SET @RelationCursor = (SELECT [fms].[dbo].[Relation].[RELATIONCODE], [fms].[dbo].[Relation].[COMPANYNAME] INTO @RelationCode, @CompanyName FROM [fms].[dbo].[Relation]) 

OPEN @RelationCursor 
FETCH NEXT FROM @RelationCursor INTO @RelationCode, @CompanyName 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    print(@RelationCode) 
    print(@CompanyName) 

    FETCH NEXT FROM @RelationCursor INTO @RelationCode, @CompanyName 
END 

CLOSE @RelationCursor 

Ich versuche RelationCode und Companyname in @RelationCode und @Companyname zu bekommen, damit ich sie in der Cursor-Schleife verwenden. Aber ich habe einen Fehler in der SELECT Abfrage:

Msg 156, Ebene 15, Status 1, Prozedur spLoadProfits, Zeile 21
falsche Syntax nahe dem Schlüsselwort 'IN'.

Aber die Abfrage scheint mir völlig in Ordnung, und ich kann nicht scheinen, das Problem darüber herauszufinden. Hat jemand eine Idee, wie man das beheben kann?

Antwort

2

Der Name eines Cursors sollte nicht mit @ beginnen. Außerdem müssen Sie den Cursor rückgängig machen, wenn Sie damit fertig sind.

Versuchen Sie stattdessen:

DECLARE @RelationCode int, -- I guessed the data type, change if needed 
     @CompanyName varchar(100) -- I guessed the data type, change if needed 

DECLARE RelationCursor CURSOR FOR 
    SELECT [fms].[dbo].[Relation].[RELATIONCODE], [fms].[dbo].[Relation].[COMPANYNAME] 
    FROM [fms].[dbo].[Relation] 

OPEN RelationCursor 

FETCH NEXT FROM RelationCursor INTO @RelationCode, @CompanyName 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    print(@RelationCode) 
    print(@CompanyName) 

    FETCH NEXT FROM RelationCursor INTO @RelationCode, @CompanyName 
END 

CLOSE RelationCursor 
DEALLOCATE RelationCursor;