Create a table with SQL from Query result in SSMS - sql

I am trying to create a new table from a query result in SSMS
I have tried:
CREATE TABLE table2 AS
(SELECT * FROM table1
WHERE code = 'x'
ORDER BY code;)
However I am getting an error telling me:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ORDER'.
Can anyone help?

Try this:
SELECT *
INTO NEW_TABLE
FROM table1
WHERE code = 'x'
ORDER BY code;

Related

How do I do select into from another table

Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.

Contains condition over clob in sybase db

Can someone help me with the syntax of CONTAINS in Sybase, I have tried below two, and both didn't work :
Query1:
select * from test where column_1 CONTAINS('Set');
Exception:
[Code: 102, SQL State: 37000] Incorrect syntax near 'CONTAINS'.
Query2:
select * from test where CONTAINS(column_1, 'Set');
Exception:
[Code: 102, SQL State: 37000] Incorrect syntax near ')'.
Please help
you have to remove where from your statement and that will be like below
select * from test CONTAINS(column_1, 'Set');
more about contains in sybase read documentation

Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ')' while creating the table in sql

Whenever I try to create table in my selected database I get
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'
Can anybody tell me whats the error and how to overcome.
What's the error in it ?
CREATE TABLE first_name
(
)
The error is that every table needs to have at least a single column.
CREATE TABLE users
(
id int,
firstname varchar(255),
lastname varchar(255)
)

SQL Server 2008 - Merge Script not working

This merge script i'm writing isn't compiling and I believe I have the correct syntax.
MERGE into MyTable ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN
UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
INSERT (Description, ShareAmount)
VALUES (ct_value.Description, ct_value.ShareAmount);
GO
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'into'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'ct_value'.
Try
MERGE MyTable AS ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN
UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
INSERT (Description, ShareAmount)
VALUES (ct_value.Description, ct_value.ShareAmount);
I realized that I had the SQL Server 2008 R2 client, but the server is SQL Server 2005.

Store Procedure Errors

I have created a stored procedure and am receiving these errors. How do I rectify this?
Msg 156, Level 15, State 1, Procedure usbinsertbookDatainto, Line 8
Incorrect syntax near the keyword 'AS'.
Msg 137, Level 15, State 2, Procedure usbinsertbookDatainto, Line 22
Must declare the scalar variable "#currentnoofcopiesavillable".
We would have to see the actual stored procedure SQL to be sure, but the errors are as follows:
Near the AS keyword, you have mistyped / have invalid syntax near Line 8
You have not DECLAREd #currentnoofcopiesavillable. Example being:
DECLARE #currentnoofcopiesavillable int;