1
此查詢用於證明一個概念,我最終將使用該概念來定位具有特定值的所有列,然後創建名稱/值對以導出到JSON。但我卡住了。如何在Sybase更新語句中使用變量
我查詢了sql表中所有列的列表。然後,我想逐行瀏覽Table1中的列,並使用變量更新值來構造查詢。例如,它通過讀取列表,如果COL4 =「古文字」,那麼我想設置上校4 =「新文本」
DECLARE @c varCHAR(100)
DECLARE ReadData CURSOR
FOR SELECT cname FROM sys.syscolumns WHERE creator = 'dbserver' AND tname = 'Table1'
DECLARE @RowCount INT
SET @RowCount = (SELECT COUNT(cname) FROM sys.syscolumns WHERE creator = 'dbserver' AND tname = 'Table1')
OPEN ReadData
DECLARE @I INT // iterator
SET @I = 1 // initialize
WHILE (@I <= @RowCount)
BEGIN
FETCH NEXT ReadData INTO @c
INSERT INTO serverdb.Table2 (cname)VALUES(@c)// this works inserting all 100 columns in the cname column of Table 2
UPDATE serverdb.Table1 SET @c = 'New text' WHERE @c = 'Old text'// this fails with a syntax error. @c is not being interpreted for the query. Note: If I hard code the @c var (for testing)to a known column name, the query works as well
SET @I = @I + 1
END;
爲什麼不更新語句識別變量的值?我錯過了什麼?
很大的幫助。完美工作! – user1413312