Trying to add a column to SQL Server table - sql

I am getting incorrect syntax errors with the following statement (SQL Server 2017)
USE ToDo
GO
ALTER TABLE tasks ADD COLUMN completed TINYINT(1);
Started executing query at Line 1
Commands completed successfully.
20:31:38Started executing query at Line 3
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'COLUMN'.
A very similar syntax was used to remove a column that worked fine.

This code would run in MySQL - but not on SQL Server. Consider:
alter table tasks add completed tinyint;
Rationale:
alter table does not support column in SQL Server; just remove that keyword
the tinyint datatype does not take a length

Related

Early execution of "sp_rename" causes query to fail

I'm having a strange problem with an MSSQL Query that I'm trying to run in Microsoft SQL Server 2014. It is an update script for my database structure. It should basically rename a Column (from Price to SellingPrice) of a Table after its content was merged to another one.
USE db_meta
GO
DECLARE #BakItemPrices TABLE
(
ItemNum int,
Price int,
CashPrice int
)
-- backup old prices
insert into #BakItemPrices
select ItemNum, Price from dbo.ItemInfo
-- merge into other table
alter table ShopInfo
add column Price int NOT NULL DEFAULT ((0))
update ShopInfo
set ShopInfo.Price = i.Price
from ShopInfo s
inner join #BakItemPrices i
on s.ItemNum = i.ItemNum
GO
-- rename the column
exec sp_rename 'ItemInfo.Price', 'SellingPrice', 'COLUMN' -- The Debugger executes this first
GO
This query always gave me the error
Msg 207, Level 16, State 1, Line 13
Invalid column name 'Price'.
I couldn't understand this error until I debugged the query. I was amazed as I saw that the debugger wont even hit the breakpoint I placed at the backup code and says that "its unreachable because another batch is being executed at the moment".
Looking further down I saw that the debugger instantly starts with the exec sp_rename ... line before it executes the query code that I wrote above. So at the point my backup code is being executed the Column is named SellingPrice and not Price which obviously causes it to fail.
I thought queries get processed from top to bottom? Why is the execute sequence being executed before the code that I wrote above?
Script is sequenced from top to down. But some changes to schema is "visible" after the transaction with script is committed. Split your script into two scripts, it can help.

Error on Table Name [duplicate]

This question already has answers here:
Incorrect Syntax near keyword 'Transaction' SQL Insertion Statements
(3 answers)
Closed 7 years ago.
I got an error while writing a query to create a table in SQL Server 2008 R2.
My script:
CREATE TABLE transaction
(
);
The table name is according to my client's request so I can't change the table name. I could not figure it out why this name is not working.
The error is:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'transaction'.
Bacause Transaction is a keyword you must delimit it with [] or "".
e.g. CREATE TABLE [Transaction] (...)
e.g CREATE TABLE "Transaction" (...)
Note 1: You better not use keywords for identifiers.
Note 2: Double Quotes will work as long as the QUOTED_IDENTIFIER is ON (msdn)

OPENQUERY throws error when used with WIN2K8\SQL2K12

I'm trying the following Sql query to move my stored procedure result into table
SELECT *
INTO #tmpTable
FROM OPENQUERY(WIN2K8\SQL2K12, 'EXEC vcs_gauge #gauge_name=vs1_bag,#first_rec_time=2014-09-01 09:00:00,#last_rec_time=2014-09-01 10:00:00')
following error is thrown, when I execute the query.
Incorrect syntax near '\'.
I don't want to add linked server .How to resolve this issue?
EDIT1
When I do [win2k8\sql2k12], and first execute the following command
EXEC sp_serveroption 'YourServer', 'DATA ACCESS', TRUE
A new message comes
OLE DB provider "SQLNCLI11" for linked server "WIN2K8\SQL2K12" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
You need to enclose DATETIME values in single quotes. And since your query is in a string itself, those single-quotes need to be doubled / escaped as follows (and you should probably also put the first parameter's value in escaped-single-quotes as it is clearly a string).
You should also fully qualify the stored procedure name with [DatabaseName].[SchemaName]..
And since the vcs_gauge proc uses Dynamic SQL, you need to specify the WITH RESULT SETS clause. For more info on this clause, please see the MSDN page for EXECUTE.
SELECT *
INTO #tmpTable
FROM OPENQUERY([WIN2K8\SQL2K12],
N'EXEC [DatabaseName].[SchemaName].vcs_gauge
#gauge_name = ''vs1_bag'',
#first_rec_time = ''2014-09-01 09:00:00'',
#last_rec_time = ''2014-09-01 10:00:00''
WITH RESULT SETS ( { column_specification} );
');

Syntax error trying to see sys.dm_fts_index_keywords

Trying to run
SELECT *
FROM sys.dm_fts_index_keywords( DB_ID('database_name'), OBJECT_ID('table_name') )
against the table with the index and I get a syntax error
If I run in on master I get
Msg 30007, Level 16, State 1, Line 1
Parameters of dm_fts_index_keywords and dm_fts_index_keywords_by_document cannot be null.
I had to upgrade the compatibility level for the Database in order to be able to create the stoplist. Then all worked fine. Edetails here http://blogs.msdn.com/b/sqlserverfaq/archive/2011/08/31/creating-stoplist-on-database-of-compatibility-80-90-in-sql-server-2008.aspx.

TABLE DROP statement

I am trying to drop a table that I created in SQL Server. I typed the command as follows:
DROP TABLE [Exercise2].
I received the following error message:
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Exercise2', because it does not exist or you do not have permission.
I have dropped multiple tables trying to clean out my database, but this one will not drop and I'm not sure why.