Incorrect syntax near 'REPLACE' with MSSQL - sql

I have migrated an oracle database to Microsoft SQL Server via liquibase but there are still some SQL statements that don't work. This one looked like this in oracle:
CREATE OR REPLACE VIEW "BP_RESULTS_VIEW" (
BP_ID,
RES_NAME,
RES_LONG_NAME,
MEDIAN,
LOW_HINGE,
HIGH_HINGE,
H_SPREAD,
INNER_FENCE_LOW,
INNER_FENCE_HIGH,
OUTER_FENCE_LOW,
OUTER_FENCE_HIGH,
LOW_NOTCH,
HIGH_NOTCH,
LOW_ADJACENT_VALUE,
HIGH_ADJACENT_VALUE)
AS
SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX';
After migrating it to Microsoft SQL Server it looks like this:
CREATE OR REPLACE FORCE VIEW BP_RESULTS_VIEW (BP_ID, RES_NAME, RES_LONG_NAME, MEDIAN,
LOW_HINGE, HIGH_HINGE, H_SPREAD, INNER_FENCE_LOW, INNER_FENCE_HIGH,
OUTER_FENCE_LOW, OUTER_FENCE_HIGH, LOW_NOTCH, HIGH_NOTCH,
LOW_ADJACENT_VALUE, HIGH_ADJACENT_VALUE) AS SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX'
GO
But when I want to execute it always this error occurs:
Incorrect syntax near 'REPLACE'.
I don't understand why because the REPLACE statement exists in SQL Server too. It also seems like it doesn't recognize the CAST command. I am using Microsoft SQL Server Management Studio 17

In SQL Server 2016 SP1 and later (including Azure SQL Database), use CREATE OR ALTER VIEW for the equivalent functionality. In earlier SQL Server versions, one must first drop the view and then CREATE VIEW and GRANT permissions.

SQL Server doesn't support CREATE OR REPLACE VIEW.
Instead, create the view the first time. Then simply use ALTER VIEW. That is the simplest method. You can also drop the view and the re-create it.

Related

"Incorrect syntax near" with Ip address in query in SQL Server 2012

I have tried create new view using an IP address in the query syntax. But this resulted in an error.
Query in View Editor:
SELECT *
FROM IP-ADDRESS.DATABASE.dbo.TABLE
Error:
In Normal Query (Work it):
How may I correctly execute this query?
Thank!
Try using tsql instead of the UI. I tested in the UI and I see what you mean that it is not keeping brackets.
CREATE VIEW [YourViewName]
AS
SELECT
*
FROM [IP-ADDRESS].DATABASE.dbo.TABLE
WHERE Something

not able to copy data from one table to another in sql server

i am using new installed sql server 2008
i am trying to copy data from one table to another..i wrote query like this;
insert into Clr select * from Color_tbl
but this is showing error like this:
Invalid object name 'Clr'.
what i have to do? i have change any setting?
clr is not an existing table..i hop if i execute this query that will create clr table automatically...
I used to copy data from one table to another table like this: but i dont know what happend after installing new Sql server??
any help is very appriciable
Without knowing anything else about your database and schemas, you could try using fully qualified names, like so:
INSERT INTO [DBContainingClr].[SchemaContainingClr].Clr
SELECT * FROM [DBContainingColor_tbl].[SchemaContainingColor_tbl].Color_tbl
UPDATE: OP has clarified that he is in fact trying to CREATE the table Clr, and it doesn't exist at the moment. In which case the syntax should be:
SELECT * INTO Clr FROM Color_Tbl

How do I pass system variable value to the SQL statement in Execute SQL task?

SSIS 2008. Very simple task. I want to retrieve a System Variable and use it in an SQL INSERT. I want to retrieve the value of System:MachineName and use it in an insert statement.
Using the statement INSERT INTO MYLOG (COL1) SELECT #[System::MachineName] gives the error Error: ..failed to parse. Must declare the scalar variable "#"
Using the statements SELECT #System::MachineName or SELECT ##[System::MachineName] gives the error 'Error Incorrect systax near '::'
I am not trying to pass a parameter to the query. I have searched for a day already but couldn't find how to do this one simple thing!
Here is one way you can do this. The following sample package was created using SSIS 2008 R2 and uses SQL Server 2008 R2 as backend.
Create a sample table in your SQLServer database named dbo.PackageData
Create an SSIS package.
On the SSIS, add an OLE DB connection manager named SQLServer to connect to your database, say to an SQL Server database.
On the Control flow tab, drag and drop an Execute SQL Task
Double-click on the Execute SQL task to bring the Execute SQL Task Editor.
On the General tab of the editor, set the Connection property to your connection manager named SQLServer.
In the property SQLStatement, enter the insert statement INSERT INTO dbo.PackageData (PackageName) VALUES (?)
On the Parameter Mapping tab, click Add button, select the Package variable that you would like to use. Change the data type accordingly. This example is going to insert the PackageName into a table, so the Data Type would be VARCHAR. Set the Parameter Name to 0, which indicates the index value of the parameter. Click OK button.
Execute the package.
You will notice a new record inserted into the table. I retained the package name as Package. That's why the table
Hope that helps.
Per my comment against #ZERO's answer (repeated here as an answer so it isn't overlooked by SSIS newcomers).
The OP's question is pretty much the use case for SSIS property expressions.
To pass SSIS variables into the query string one would concatenate it into an expression set for the SqlStatementSource property:
"INSERT INTO MYLOG (COL1) SELECT " + #[System::MachineName]
This is not to suggest the accepted answer isn't a good pattern, as in general, the parameterised approach is safer (against SQL injection) and faster (on re-use) than direct query string manipulation. But for a system variable (as opposed to a user-entered string) this solution should be safe from SQL injection, and this will be roughly as fast or faster than a parameterised query if re-used (as the machine name isn't changing).
I never use it before but maybe you can check out the use of expression in Execute SQL task for that.
Or just put the whole query into an expression of a variable with evaluateAsExpression set to true. Then use OLE DB to do you insert
Along with #user756519's answer, Depending on your connection string, your variable names and SQLStatementSource Changes

SQL Server character for enclosing database, table, field names

I'm writing a script to create a bunch of tables, and I read in the Microsoft documentation that I should use tick marks (aka grave) to enclose database, table, and field names, but when I run it in SQL Server Management Studio, I get a syntax error on the first tick:
CREATE TABLE `active`.`test` ( … )
^syntax error
So I tried running it thru a lint, and it told me that ` is an invalid character, and it suggested removing them, which totally messed up the script.
What gives?
Use square brackets...
CREATE TABLE [active].[test](...)
The documentation you have linked to is:
The SQL query strings for Windows Installer are restricted to the following formats.
This is not the syntax for SQL Server. I suggest looking at the Transact-SQL Reference instead.
You need to use [] instead of the backtick:
CREATE TABLE [active].[test]

SQL to add column with default value - Access 2003

Updating an old ASP/Access site for a client - I need SQL to add a column to an existing table and set a default value. Doesn't work - any ideas?
This works fine
ALTER TABLE documents ADD COLUMN membersOnly NUMBER
I want this to work:
ALTER TABLE documents ADD COLUMN membersOnly NUMBER DEFAULT 0
Have googled and seen instructions for default values work for other field types but I want to add number. Thanks!
Tools -> Options -> Tables/Queries -> (At the bottom right:) Sql Server Compatible Syntax - turn option on for this database.
then you can execute your query:
ALTER TABLE documents ADD COLUMN membersOnly NUMBER DEFAULT 0
With ADO, you can execute a DDL statement to create a field and set its default value.
CurrentProject.Connection.Execute _
"ALTER TABLE discardme ADD COLUMN membersOnly SHORT DEFAULT 0"
How are you connecting to the database to run the update SQL? You can use the ODBC compatible mode through ADO. Without opening the database in Access.
You may find Sql Server Compatible Syntax is already turned on, so definately worth just trying to run the sql statement mentioned above (via an ADO connection from ASP) before resorting to taking the db offline. Thanks, this helped me out.
Tools -> Options -> Tables/Queries -> (At the bottom right:) Sql Server Compatible Syntax - turn option on for this database.
is not found on MS Access 2010