您發佈的代碼沒有任何問題。所以它必須是別的東西。 (例如,電視機遙控器是好的,但電視沒有插入)
1)檢查在proc的代碼是真的你認爲它是
sp_helptext 'GetValues'
2)確保有隻有一個GetValues,而你沒有使用另一個用戶,例如asmo.GetValues
select * from information_schema.routines where specific_name= 'GetValues'
3)如果失敗則從proc中獲取SQL。對參數值進行硬編碼並運行,看它是否返回您認爲應該的結果。如果它不會將該sql發佈到SO。
編輯根據您添加的表格,我嘗試複製您遇到的問題,但SQL中的表名和數據名稱不一樣,數字也不相同。所以我試圖綜合這兩個。以下工作。我希望它可以幫助
Create Database Test
GO
USE Test
GO
CREATE Table Forms
(FormID int,
FormName nvarchar(50))
GO
Create Table Pools
(PoolID int,
FormID int,
PoolName nvarchar(50))
GO
Create Table Records
(RecordID int,
PoolID int)
GO
Create Table [Values]
(FieldID int,
RecordID int,
FieldValue nvarchar(50))
GO
CREATE Table [Fields]
(
FieldID int,
FieldName nvarchar(100)
)
GO
CREATE Table [FormFields]
(
FieldID int,
FormID int
)
GO
INSERT INTO Forms (FormID, FormName) VALUES (96, 'FormA')
INSERT INTO Forms (FormID, FormName) VALUES (98, 'FormB')
INSERT INTO Forms (FormID, FormName) VALUES (97, 'FormC')
GO
INSERT INTO Pools (PoolID, FormID, PoolName) Values (29, 96, 'PoolA1')
INSERT INTO Pools (PoolID, FormID, PoolName) Values (31, 98, 'PoolB1')
INSERT INTO Pools (PoolID, FormID, PoolName) Values (30, 97, 'PoolC1')
GO
INSERT INTO Records (RecordID, PoolID) Values (42, 29)
INSERT INTO Records (RecordID, PoolID) Values (43, 29)
INSERT INTO Records (RecordID, PoolID) Values (44, 29)
INSERT INTO Records (RecordID, PoolID) Values (45, 31)
INSERT INTO Records (RecordID, PoolID) Values (46, 31)
INSERT INTO Records (RecordID, PoolID) Values (47, 31)
GO
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 42, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 43, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 44, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 45, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 46, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 47, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 42, 'Smith')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 43, 'Jones')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 44, 'Fletchers')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Fred')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Bob')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Marty')
GO
INSERT INTO [Fields] (FieldID, FieldName) Values (101, 'Color')
INSERT INTO [Fields] (FieldID, FieldName) Values (102, 'Last Name')
INSERT INTO [Fields] (FieldID, FieldName) Values (103, 'First Name')
GO
INSERT INTO FormFields (FieldID, FormID) Values (101, 96)
INSERT INTO FormFields (FieldID, FormID) Values (102, 96)
INSERT INTO FormFields (FieldID, FormID) Values (103, 96)
INSERT INTO FormFields (FieldID, FormID) Values (101, 97)
INSERT INTO FormFields (FieldID, FormID) Values (102, 97)
INSERT INTO FormFields (FieldID, FormID) Values (103, 97)
INSERT INTO FormFields (FieldID, FormID) Values (101, 98)
INSERT INTO FormFields (FieldID, FormID) Values (102, 98)
INSERT INTO FormFields (FieldID, FormID) Values (103, 98)
GO
Create PROCEDURE [dbo].[GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)
AS
SELECT
v.FieldValue
FROM
dbo.Forms f
INNER JOIN dbo.Pools p ON f.FormID = p.FormID
INNER JOIN dbo.FormFields ff on f.FormID = ff.FormID
INNER JOIN dbo.Fields fd on fd.FieldID = ff.FieldID
INNER JOIN dbo.Records r on p.PoolID = r.PoolID
INNER JOIN dbo.[Values] v on r.RecordID = v.RecordID
and ff.FieldID = v.FieldID
WHERE
fd.FieldName = @FieldName
and f.FormName = @FormName
AND p.PoolName = @PoolName
GO
dbo.getValues 'Color', 'FormA', 'PoolA1'
因此,無論你放入@FormName和@PoolName,他們都被忽略?這看起來不正確。 – LittleBobbyTables 2010-08-26 15:55:47
確實,它似乎總是從所有窗體和所有池中選擇值。 – asmo 2010-08-26 16:00:09