This question already has an answer here:
How to set 'start with' of sequence to select query result in SQL server?
(1 answer)
Closed 5 years ago.
I'd like to make it so, that a sequence starts with the max value(+1) of a certain column. This is what I've got so far:
SELECT MAX(customer_number)+1 AS HighestCustomerNumberPlusOne FROM organisation;
CREATE SEQUENCE customer_number_sequence
START WITH 1
INCREMENT BY 1;
ALTER SEQUENCE customer_number_sequence RESTART WITH HighestCustomerNumberPlusOne;
And I get the error:
Error: Incorrect syntax near 'HighestCustomerNumberPlusOne'.
SQLState: S0001
ErrorCode: 102
What is the correct syntax? I'm using SQL Server 2012 but would prefer just generic SQL if it's possible, since the T-SQL is harder than learning Chinese.
ALTER SEQUENCE syntax accepts only constants in the RESTART WITH clause.
So, you'll have to save the result of your query into a variable; construct a string with ALTER SEQUENCE SQL command and embed the value of the variable in the SQL text, then execute it as dynamic SQL using EXEC or sp_executesql.
But, normally there is no need to reset the sequence.
What do you really need?
Related
This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection
My RDMS is the above. I have a stored procedure that takes 13 parameters. I have a table that holds those input values. I am pasting what I have done. Please help because its not executing. I am getting the following error: Incorrect syntax near '.'.
Please look at the attachment to see the location of the table I am trying to use.enter image description here
use ssisconfiguration.dbo.BalladHealthDayOneStats;
exec Clarity.ssis.sp_BalladDayOneMaster
[StatisticStartDate],[StatisticEndDate],[CostCenterList],[ProcedureCodeList],[Statistic],[StatisticDescription],[PatientTypeIndicator],
--[RVThreshold],[FiscalYear],
1,2017,
[PatientServiceList],[PatientClassList],[PatientStatusList]
The purpose of USE is to select the database.
i would suggest modify to
use ssisconfiguration
exec Clarity.ssis.sp_BalladDayOneMaster
([StatisticStartDate],[StatisticEndDate],[CostCenterList],[ProcedureCodeList],[Statistic],[StatisticDescription],[PatientTypeIndicator],
--[RVThreshold],[FiscalYear],
1,2017,
[PatientServiceList],[PatientClassList],[PatientStatusList])
replace the column name with values in the []
I am running a select statement which contains the following CASE clause:
SELECT
(CASE MyTable.IsBook WHEN 1 THEN 'B' ELSE 'M' END) AS IsBookOrManuscript
FROM MyTable;
I have the same exact database(schema and data) restored in two different physical servers running SQL Server 2008 R2 with build version 10.50.4276.0 and SQL Server 2014 respectively.
When run in SQL Server 2014 the query returns as expected. When run in SQL Server 2008R2 the error message Incorrect syntax near ' '. occurs.
Searching the script file for non-ascii characters I found that indeed, three occurrences of 0x0A character appear in the CASE line and removing it solves the problem in SQL Server 2008R2.
Does anyone know why that happens? Is it an intended behavior? As far as I can see there are 2 CUs released since my last update BUT they do not seem to fix or recognize the problem. Any thoughts?
Check the Setting of
QUOTED_IDENTIFIER
on each server.
as MSDN says:
Causes SQL Server to follow the ISO rules regarding quotation mark delimiting identifiers and literal strings. Identifiers delimited by double quotation marks can be either Transact-SQL reserved keywords or can contain characters not generally allowed by the Transact-SQL syntax rules for identifiers.
so try the next code:
SET QUOTED_IDENTIFIER off
-- Type your code here
SET QUOTED_IDENTIFIER On
This question already has answers here:
SQL Query to add a new column after an existing column in SQL Server 2005
(7 answers)
Closed 7 years ago.
I am still finding my feet in SQL Server, and I am trying to do a simple column addition and it is throwing an error. I am trying to add the KinAdr3 column after the KinAdr2 column but it is throwing the following error
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'AFTER'.
Below is the SQL statement I am trying to execute
ALTER TABLE TBL_MEDICAL_Patient
ADD KinAdr3 nVARCHAR(50)
AFTER KinAdr2
Thanks in advance
The reason is simple as the above syntax is valid in MySQL but is not valid in SQL Server. In SQL Server following syntax is valid:
ALTER TABLE tablename ADD columnname INT
However, a user wanted to add the column between two of the columns. SQL Server is relational engine. The order of the column should not matter in any of the T-SQL operations. It does not matter in most of the cases (except when the table is extra large, and it has many NULL columns it impacts the size of the table). In reality whenever user wants to add a column to the table, he/she should just the column and later retrieve the column in a specific order in the table using column names.
It is always a good idea to specify the name of the column in the T-SQL query (using * is indeed a bad idea but that is out of scope of this blog post).
For more details please see the SQL SERVER – How to Add Column at Specific Location in Table
This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 8 years ago.
As you see from these images, the ROSHEADERID column is invalid and when I execute the first one it returns a meaningful message. But when I use this query in where clause as a subquery. It executes and deletes all of the records without warning or aborting the operation.
How can this be ?
The subquery has access to columns in outer query, thus column ROSHEADERID you ask for is effectively taken from EXM_REVIEWOFSYSTEMS (not from EXM_REVIEWOFSYSTEMSHEADER), thus deleting all records in outer table.
This should clarify a bit on what's going on behind the scenes:
http://sqlfiddle.com/#!2/623c7/3
More information here: http://technet.microsoft.com/en-us/library/ms187638(v=sql.105).aspx
Use Alias Names for tables to avoid conflict:
BEGIN TRAN
DELETE FROM EXM_REVIEWOFSYSTEMS
WHERE ROSHEADERID IN ( SELECT rsh.ROSHEADERID
FROM EXM_REVIEWOFSYSTEMSHEADER rsh
WHERE rsh.PATIENTID = '' )
ROLLBACK TRAN