MS SQL Server Compact Edition 4.0中插入语句值中的2个子查询

问题描述 投票:0回答:1

具体来说,我正在使用SQL Server Compact 4.0,如果这有所不同。我有3个表(注意,userTable,诗歌)。除了在此注释表中,用户和verse表没有相关性,因此我无法执行连接这两个表的单个子查询。

INSERT INTO [note]
           ([verse_id]
           ,[user_id]
           ,[text]
           ,[date_created]
           ,[date_modified])
     VALUES
           ( (SELECT Id FROM verse 
                WHERE volume_lds_url = 'ot' 
                AND book_lds_url = 'gen' 
                AND chapter_number = 8
                AND verse_number = 16)
           , (SELECT Id FROM userTable
                WHERE username = 'canichols2')
           ,'test message'
           ,GETDATE()
           ,GETDATE());
GO

据我所知,该声明应该有效。如果我对外键值进行硬编码,则外部语句可以正常工作,并且每个子查询都可以正常工作,并且每个子查询只返回一列和一行。

错误消息:There was an error parsing the query. [ Token line number = 8,Token line offset = 14,Token in error = SELECT ]

所以它不喜欢标量值子句中的子查询,但我无法弄清楚如何使用

INSERT INTO .... SELECT ....

声明与2个不同的表。


表定义

由于@Prasanna要求它,这里是定义

CREATE TABLE [userTable] (
  [Id] int IDENTITY (1,1)  NOT NULL
, [username] nvarchar(100)  NOT NULL
, [email] nvarchar(100)  NOT NULL
, [password] nvarchar(100)  NULL
);
GO
ALTER TABLE [userTable] ADD CONSTRAINT [PK_user] PRIMARY KEY ([Id]);
GO
CREATE TABLE [note] (
  [Id] int IDENTITY (1,1)  NOT NULL
, [verse_id] int  NULL
, [user_id] int  NULL
, [text] nvarchar(4000)  NOT NULL
, [date_created] datetime DEFAULT GETDATE() NOT NULL
, [date_modified] datetime NULL
);
GO
ALTER TABLE [note] ADD CONSTRAINT [PK_note] PRIMARY KEY ([Id]);
GO
CREATE TABLE [verse] (
  [Id] int IDENTITY (1,1)  NOT NULL
, [volume_id] int  NULL
, [book_id] int  NULL
, [chapter_id] int  NULL
, [verse_id] int  NULL
, [volume_title] nvarchar(100)  NULL
, [book_title] nvarchar(100)  NULL
, [volume_long_title] nvarchar(100)  NULL
, [book_long_title] nvarchar(100)  NULL
, [volume_subtitle] nvarchar(100)  NULL
, [book_subtitle] nvarchar(100)  NULL
, [volume_short_title] nvarchar(100)  NULL
, [book_short_title] nvarchar(100)  NULL
, [volume_lds_url] nvarchar(100)  NULL
, [book_lds_url] nvarchar(100)  NULL
, [chapter_number] int  NULL
, [verse_number] int  NULL
, [scripture_text] nvarchar(4000)  NULL
);
GO
ALTER TABLE [verse] ADD CONSTRAINT [PK_scriptures] PRIMARY KEY ([Id]);
GO

我知道它不是第一种正常形式或任何东西,但这是给我的方式,我不想把它分成多个表格。

SubQuery Results

显示结果以及如何只有1行。

SELECT Id FROM WHERE volume_lds_url = 'ot'
    AND book_lds_url = 'gen' 
    AND chapter_number = 8
    AND verse_number = 16

ID 200

和第二个子查询

SELECT Id FROM userTable
    WHERE username = 'canichols2'

ID 1

sql-server sql-server-ce-4 in-subquery
1个回答
1
投票

注意:目标系统是SQL-Server-Compact-CE-4

这个较小的兄弟似乎不支持子选择作为标量值,也不支持声明变量。在评论中查找详细信息

方法1

只要你可以肯定,子选择只返回一个标量值,你应该很容易将你的VALUES转换为SELECT。试试这个:

INSERT INTO [note]
       ([verse_id]
       ,[user_id]
       ,[text]
       ,[date_created]
       ,[date_modified])
 SELECT
        (SELECT Id FROM verse 
            WHERE volume_lds_url = 'ot' 
            AND book_lds_url = 'gen' 
            AND chapter_number = 8
            AND verse_number = 16)
       , (SELECT Id FROM userTable
            WHERE username = 'canichols2')
       ,'test message'
       ,GETDATE()
       ,GETDATE();

方法2

没有使用Compact-SQL-Server的经验,但您可以尝试这样做:

DECLARE @id1 INT=(SELECT Id FROM verse 
            WHERE volume_lds_url = 'ot' 
            AND book_lds_url = 'gen' 
            AND chapter_number = 8
            AND verse_number = 16);

DECLARE @id2 INT=(SELECT Id FROM userTable
            WHERE username = 'canichols2');

INSERT INTO [note]
       ([verse_id]
       ,[user_id]
       ,[text]
       ,[date_created]
       ,[date_modified])
 SELECT @id1
       ,@id2
       ,'test message'
       ,GETDATE()
       ,GETDATE();
© www.soinside.com 2019 - 2024. All rights reserved.