Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Variabeln in Statements

Empfohlene Antworten

Veröffentlicht

Hallo Allerseits,

ich habe mit MSSQL die Schwierigkeit eine vorher definierten Variabel in einen simplen SELECT-Statement zu verarbeiten. Dieser Variablen soll zur Laufzeit ein Tabellennamen zugewiesen werden. Die Variabel soll später Bestandteil der SELECT-Statements sein. Beispiel:


-- Deklarieren der Variabel

DECLARE @cTabelle


-- Zuweisung 


SET @cTabelle = 'Testtabelle'


-- SQL-Statement


SELECT * FROM @cTabelle


funktioniert leider so nicht, weil es eine Fehlermeldung gibt 'Variabel: @cTabelle nicht deklariert!'

Wo liegt der Fehler..

Kann irgendeiner mir helfen?

Erstens muss du der Variablen einen Werttyp zuweisen.

Zweitens, willst du wahrscheinlich dynamisches SQL betreiben. So wie du es versuchst, wird es nicht funktionieren. Du darfst nicht dynamisches und statisches SQL mischen.

versuch's mal mit


-- Deklarieren der Variabel

DECLARE @cTabelle varchar(25)

DECLARE @cSelect varchar(50)


-- Zuweisung 

SET @cTabelle = 'Testtabelle'

SET @cSelect = 'Select * From ' + @cTabelle 


-- SQL-Statement

exec(@cSelect )


Hallo Byteloser,

vielen Dank für die Hilfestellung

gruss Peter

  • 8 Monate später...

Hi,

also bei mir tritt beim ausführen folgende Fehlermeldung auf:

DECLARE @cJahr number(4)

DECLARE @cMonat number(2)

DECLARE @cSelect varchar(100)


SET @cJahr = 2009

SET @cMonat = 3

SET @cSelect = 'Select * from lz_doku where jahr = @cJahr and monat = @cMonat'


exec(@cSelect);

 11:16:50  [DECLARE - 0 row(s), 0.000 secs]  [Error Code: 6550, SQL State: 65000]  ORA-06550: Zeile 1, Spalte 9:

PLS-00103: Fand das Symbol "@" als eines der folgenden erwartet wurde:


   begin function package pragma procedure subtype type use

   <an identifier> <a double-quoted delimited-identifier> form

   current cursor

Das Symbol "@" wurde ignoriert.

ORA-06550: Zeile 2, Spalte 1:

PLS-00103: Fand das Symbol "DECLARE" als eines der folgenden erwartet wurde:


   := ; not null default character

... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]


Woran kann das liegen?

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.