0

Specifically I'm using SQL Server Compact 4.0, if that makes a difference. I have 3 tables (note,userTable,verse). the user and verse table have no correlation except in this note table, so I can't do a single subquery joining the two tables.

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

As far as I can tell, the statement should work. The outer statements works fine if i hard code the Foreign Key values, and each of the subqueries work as they should and only return one column and one row each.

Error Message:There was an error parsing the query. [ Token line number = 8,Token line offset = 14,Token in error = SELECT ]

So It doesn't like the subquery in a scalar values clause, but I Can't figure out how to use a

INSERT INTO .... SELECT ....

statement with the 2 different tables.


Table Definitions

Since @Prasanna asked for it, here's the deffinitions

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

I'm aware it's not in the 1st normal form or anything, But that's how it was given to me, and I didn't feel like dividing it up into multiple tables.

SubQuery Results

To show the results and how there's only 1 row.

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

Id
200

And the second subquery

SELECT Id FROM userTable
    WHERE username = 'canichols2'

Id
1

Community
  • 1
  • 1

1 Answers1

1

Attention: The target system is SQL-Server-Compact-CE-4

This smaller brother seems not to support neither sub-selects as scalar values, nor declared variables. Find details in comments...

Approach 1

As long as you can be sure, that the sub-select returns exactly one scalar value, it should be easy to transform your VALUES to a SELECT. Try this:

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();

Approach 2

No experience with Compact editions of SQL-Server, but you might try this:

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();
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • I tried that as well, still gives the same error. I even went a step further and made the first subquery the outer select statement, and just kept the second subquery alone. The error then went to the other subquery, but same error. – Cody Nichols Mar 06 '18 at 08:51
  • Which version of sql-server are your using? I cannot test this at the moment, but it should work... A sub-select in paranthesis is treated as a scalar value - as long as it returns one single value in one single row (-> scalar value). – Shnugo Mar 06 '18 at 08:54
  • Sorry, just saw, that's SQL Server Compact 4.0. No experience with this... You might use declared variables first to retrieve the values, then to insert them. – Shnugo Mar 06 '18 at 08:55
  • I also tried that. From what I've read, CE doesn't support declared variables. it gives an error with the 'DECLARE' keyword now instead of 'SELECT'. Definitely my problems are stemming from having to use CE. I wonder how they expect anyone to get the PK of another table with CE. – Cody Nichols Mar 06 '18 at 09:06
  • @CodyNichols According [to this related question](https://stackoverflow.com/a/1058207/5089204) SQL-Server-CE is capable of using a sub-select with a `JOIN`. Try it out... – Shnugo Mar 06 '18 at 09:11