Set a value equal to the value of a sum from a row above?

I have a table like this Opening Transfer 0 12 12 -2 10 -2 8 -1 7 -7 I need to recalculate this due to a gap in the real table. I know the value of the opening will be the opening + transfer of the line above but how can I do this? I’ve… Read More Set a value equal to the value of a sum from a row above?

T SQL variable showing incorrect syntax

DECLARE @tablename VARCHAR(100) = ‘House’; IF OBJECT_ID(@tablename, N’U’) IS NOT NULL BEGIN IF EXISTS(SELECT 1 FROM @tablename) BEGIN PRINT ‘Table already exists and has data, not dropping it.’ END ELSE BEGIN DROP TABLE @tablename PRINT ‘Table dropped successfully.’ END END ELSE BEGIN PRINT ‘Table does not exist.’ END Please correct it for SQL Server >Solution… Read More T SQL variable showing incorrect syntax

Add week number column in query results from existing date column

I am just learning SQL, using SQL Server Management Studio. The current query is shown below and I am struggling with trying to add a column to my results query, where it converts the PRODUCTION_DATE into fiscal week. SELECT ORDER_QTY, SKU, INVOICE_NUMBER, CUSTOMER_NUMBER, ROUTE, ALLOCATED_QTY, SHORTED_QTY, PRODUCTION_DATE FROM [DATEBASE_NAME].[XYZ].[ORDERS] WHERE [PRODUCTION_DATE] >= DATEADD(day, -300, GETDATE())… Read More Add week number column in query results from existing date column

Incorrect syntax near the keyword 'ON' – SQL Server 2019

Can anyone tell me why the following syntax is resulting in error? I would like to update the ip address field with dynamic ip addresses if a current record exists with a matching primary key. INSERT INTO Overview([Computername], [IpAddress]) VALUES(‘test’, ‘127.0.0.1’) ON DUPLICATE KEY UPDATE Overview set IpAddress = ‘127.0.0.1’; >Solution : There is no… Read More Incorrect syntax near the keyword 'ON' – SQL Server 2019

Add foreign column with default value

I need to add a foreign key column in an existing table using alter statement. And need to set default value to insert default in case of null. For that I added below alter query: ALTER TABLE COM.USER_GENERATED_TASK_LIST ADD Application_ID INTEGER NOT NULL CONSTRAINT DK_Application_ID DEFAULT (5) WITH VALUES, CONSTRAINT FK_Application_ID FOREIGN KEY (Application_ID) REFERENCES… Read More Add foreign column with default value