Dropping view error - sql-server

Why this returns error:
DROP VIEW vTest; GO
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'GO'.
And this works fine:
DROP VIEW vTest;
GO
And how to adjust first statement so that it runs fine on the same line?

If you're passing SQL to SQL Server via an API, you do not need the "GO". The "GO" is a feature of OSQL.exe, ISQL.exe, SQLCMD.exe, and SQL Server Management Studio. It's not a feature of SQL Server or T-SQL. So if you're coding in C# or javascript or Ruby or Perl or whatever, and you're just trying to execute some SQL via an API... just pass the SQL, no "GO" commands, and it'll work for you.
The "GO" is just a "batch separator", separating batches of SQL to be sent to SQL Server. It's utterly unnecessary here, when you have only one batch.

Related

Using FORCESEEK on SQL Server query

I was using this option for a long time on specific query, all of the sudden SQL Server throws this error:
Msg 8622, Level 16, State 1, Line 2
Query processor could not produce a query plan because of the hints defined in this query. Resubmit the query without specifying any hints and without using SET FORCEPLAN.
What could be the reason for this error to be generated at this moment if nothing was changed in the stored procedure? I know is not recommended to used I just want to know why is working in one server but not in another?

SQL Server 2016 Stored Procedure Syntax Error

I recently restored a SQL Server 2008 database into an instance of SQL Server 2016. Most of the functionality seems to work fine, but many of my stored procedures that include updates to an application database table called SYS_USER fails with the following error:
Msg 102, Level 15, State 1, Procedure SYS_USERupdate, Line 35 [Batch Start Line 0]
Incorrect Syntax near '#errorNumber'
The database does not have a stored procedure called SYS_Userupdate and none of the procedures' code includes the term #errorNumber. I attempted to run the SQL query from one of the failing procedures directly in SQL Server Management Studio and received the same error message. Here is the SQL query that is failing:
UPDATE SYS_USER
SET SYS_USER_LGF_DT = GETDATE()
WHERE SYS_USER_ID = #SYS_USER_ID
I plugged in a valid value for the #SYS_USER_ID variable. Similar queries in related to other tables run without an issue. All of the stored procedures work on a SQL Server 2008 instance with no errors. Also, the database compatibility_level to 100, which should be acceptable for SQL Server 2016.
This happens when you're not running on a correct version of SQL Server, or if the compatibility level of the database isn't set sufficiently. So change it to 130
To check compatibility level:
select compatibility_level
from sys.databases
where name = '<database name>'
To alter compatibility level:
alter database <database-name>
set compatibility level = 130 -- SQL Server 2016
Compatibility levels list for all SQL versions: ALTER DATABASE (Transact-SQL) Compatibility Level
Reposting the answer since it was proposed in a comment to the question. The solution to the problem was that there were triggers on the affected tables that I did not know were there. Syntax errors on these triggers were causing insert and update queries on the tables to fail.
Credit to Jeroen Mostert https://stackoverflow.com/users/4137916/jeroen-mostert for helping with this!

Error while execute db to server

Have tried for hours to execute my db to my server, but keeps getting the same error.
What I did first what on my local dbd i did go to tasks -> generate scripts where I under scripts options chose schema and data. After the script was generated, I logged in to my hosts server, where i dragged the database in to my SQL Server Management Studio. Then I removed all the following text:
Gist with text
and replaced the USE [aspnet-Billigcsgosalgssite-20170731103725] with
USE sql6003.smarterasp.net
After that I did the execute, and got the following error code:
Msg 911, Level 16, State 1, Line 1
Database 'sql6003' does not exist. Make sure that the name is entered correctly.
Msg 102, Level 15, State 1, Line 223
Incorrect syntax near '.'.
I really don't know what to do anymore, and would be happy for some help!
I think first you need to create database on your local machine
create database sql6003
After this run the script that you have taken from different server

Getting script errors when generating SQL script for Azure using SQL Server Management Studio

I am using the free azure websites and made an sql database. I need to create the db tables, and then insert the dummy in.
I tried to generate the scripts through sql management studio but I get a lot of errors when I run it against the azure db.
Msg 40508, Level 16, State 1, Line 1
USE statement is not supported to switch between databases. Use a new connection to connect to a different Database.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'CONTAINMENT'.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'COMPATIBILITY_LEVEL'.
How can I get the data into my azure database?
Just taking a guess here, based on the errors: When generating scripts via SSMS, you need to specify output specific to Windows Azure SQL Database (formerly known as SQL Azure). I don't have it installed at the moment so I can't provide you with a screenshot. I'll update my answer, should I get it installed before you verify this is indeed the issue.
as David mentioned above, you can specify the script generation to target Windows Azure SQL Database
Hi this may be a little late but I found the this link to be helpful:
You basically have to manualy change the context (click on the correct database on the list of dropdown) before executing the query. You may have noticed this when working between databases.
Hope this helps

Unknown token received from SQL Server

I got this error when trying to run a query in SQL server query analyzer.
What is the reason?
If you're using cursors, then you've probably closed the cursor. Check out MSDN for more information (http://support.microsoft.com/kb/151693)
If you're not using cursors, then it could be an issue with the server, check the log.
And before it's lost to the sands of time, the KB article:
Article ID: 151693 - Last Review: October 3, 2003 - Revision: 3.0
FIX: Err Msg "Unknown Token Received from SQL Server"
Symptoms
When you set the statement options to use a server-side cursor and prepare a select statement on a SQL Server system table, the first execution of the select creates the cursor successfully. After you close this cursor, if you execute the prepared statement again, the following error message appears:
unknown token received from SQL Server.
Workaround
Use a forward-only cursor instead of a static, keyset, or dynamic cursor on the system tables. Note that the problem does not occur if a server-side cursor is created on a user-defined table or view.
If a forward-only cursor is unacceptable, prepare the select statement again on the system table and execute it to create a server-side cursor. Note that the first execution works fine and applications typically do not need to create a cursor on a system table repeatedly.
Status
Microsoft has confirmed this to be a problem in Microsoft SQL Server version 6.50.0201. This problem has been corrected in U.S. Service Pack 1 for Microsoft SQL Server version 6.5. For more information, contact your primary support provider.

Resources