views:

91

answers:

5

I have the following query running on a MySQL, but it fails when I run it on a SQL Server database. How should it look to please SQL Server?

INSERT INTO first_table (pk, data)
VALUES ((SELECT value
           FROM second_table
          WHERE key = 'key'),
          'other-data');
+1  A: 
INSERT INTO first_table (pk, data)
SELECT value, 'other-data'
FROM second_table
WHERE key = 'key'
AdaTheDev
+1 correct answer
kevchadders
+2  A: 

Something like this

INSERT INTO first_table (pk, data) 
SELECT  value ,
        'other-data'
FROM    second_table 
WHERE key = 'key'

Have a look at INSERT

Load data using the SELECT and EXECUTE options

astander
+1 correct answer
kevchadders
A: 

You could use a subquery to retrieve the PK:

INSERT INTO first_table (pk, data)
SELECT
    (SELECT value FROM second_table WHERE key = 'key')
,   'other-data'
Andomar
+1  A: 

Try this:

INSERT INTO first_table (pk, data)
SELECT value, 'other-data'
FROM second_table
WHERE key = 'key';
Bob Pusateri
+1 correct answer, though you dont need to use have the semi colon...
kevchadders
Nope the semicolon isn't necessary, I'm just trying to get into the habit of always using one
Bob Pusateri
A: 
INSERT INTO first_table (pk, data)
SELECT value, 'other-data'
FROM second_table
WHERE key = 'key' 
kevchadders
I am suprised! 27 sec. and I got an answer (actually five).You get the points though you forgot the semicolon ;-)
homaxto
thanks though you dont need to use the semi colon in MS SQL... ;)
kevchadders