Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Change value of column in last row when doing insert Expand / Collapse
Author
Message
Posted Sunday, February 10, 2013 6:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, February 22, 2013 10:40 AM
Points: 153, Visits: 323
DECLARE @LastOne bit = 0
CREATE TABLE #tblTasks
(
TaskID int,
Task varchar(50),
LastOne bit,
)
GO

INSERT INTO #tblTasks (TaskID, Task, LastOne)
SELECT(TaskID, Task, @LastOne)
FROM tblOldTasks


When doing an insert like the one above - where I am inserting a hard-coded value of 0 for @LastOne column - is there any way of writing such that ...

For every row inserted - insert 0 (or false) as LastOne except for the last row - when LastOne should be 1.

I realise I can do it immediately after the Insert with an Update, but I wondered if there is some way to do it within the Select statement that is inserted.
Post #1418104
Posted Sunday, February 10, 2013 7:31 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 5:16 AM
Points: 85, Visits: 352
Use default argument for the column.

"DEFAULT
Specifies the value provided for the column when a value is not explicitly supplied during an insert. DEFAULT definitions can be applied to any columns except those defined as timestamp, or those with the IDENTITY property. If a default value is specified for a user-defined type column, the type should support an implicit conversion from constant_expression to the user-defined type. DEFAULT definitions are removed when the table is dropped. Only a constant value, such as a character string; a scalar function (either a system, user-defined, or CLR function); or NULL can be used as a default. To maintain compatibility with earlier versions of SQL Server, a constraint name can be assigned to a DEFAULT."

As seen here: http://msdn.microsoft.com/en-us/library/ms174979(v=sql.105).aspx



______________________________
AJ Mendo | @SQLAJ
Post #1418108
Posted Sunday, February 10, 2013 2:19 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 9:33 PM
Points: 299, Visits: 1,122
Hi

You could try the following
INSERT INTO #tblTasks
SELECT TaskID,
Task,
CASE
WHEN ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) = COUNT(*) OVER (PARTITION BY (SELECT NULL)) THEN 1
ELSE 0
END LastOne
FROM tblOldTasks

This assumes that the insert is not ordered
Post #1418139
Posted Sunday, February 10, 2013 4:34 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:10 AM
Points: 1,289, Visits: 3,858
sku370870 (2/10/2013)
DECLARE @LastOne bit = 0
CREATE TABLE #tblTasks
(
TaskID int,
Task varchar(50),
LastOne bit,
)
GO

INSERT INTO #tblTasks (TaskID, Task, LastOne)
SELECT(TaskID, Task, @LastOne)
FROM tblOldTasks


When doing an insert like the one above - where I am inserting a hard-coded value of 0 for @LastOne column - is there any way of writing such that ...

For every row inserted - insert 0 (or false) as LastOne except for the last row - when LastOne should be 1.

I realise I can do it immediately after the Insert with an Update, but I wondered if there is some way to do it within the Select statement that is inserted.


To answer that, we would need to know "which row is the last row?".

You realise that a SELECT has no guarantee of first or last row without an ORDER BY that guarantee a first or last row...?

Also, the order of a select does not reflect the order rows are inserted...that will depend on many other factors.


MM




Post #1418148
Posted Sunday, February 10, 2013 6:16 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906, Visits: 26,790
SQLAJ (2/10/2013)
Use default argument for the column.

"DEFAULT
Specifies the value provided for the column when a value is not explicitly supplied during an insert. DEFAULT definitions can be applied to any columns except those defined as timestamp, or those with the IDENTITY property. If a default value is specified for a user-defined type column, the type should support an implicit conversion from constant_expression to the user-defined type. DEFAULT definitions are removed when the table is dropped. Only a constant value, such as a character string; a scalar function (either a system, user-defined, or CLR function); or NULL can be used as a default. To maintain compatibility with earlier versions of SQL Server, a constraint name can be assigned to a DEFAULT."

As seen here: http://msdn.microsoft.com/en-us/library/ms174979(v=sql.105).aspx


Please explain how adding a default to the table will clearly and certainly make a different entry for ONLY the last row inserted.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1418161
Posted Monday, February 11, 2013 9:09 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, February 22, 2013 10:40 AM
Points: 153, Visits: 323
Thanks for that, I'll give it a try.
Post #1418506
Posted Monday, February 11, 2013 9:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:51 AM
Points: 5,608, Visits: 10,970
sku370870 (2/10/2013)
DECLARE @LastOne bit = 0
CREATE TABLE #tblTasks
(
TaskID int,
Task varchar(50),
LastOne bit,
)
GO

INSERT INTO #tblTasks (TaskID, Task, LastOne)
SELECT(TaskID, Task, @LastOne)
FROM tblOldTasks


When doing an insert like the one above - where I am inserting a hard-coded value of 0 for @LastOne column - is there any way of writing such that ...

For every row inserted - insert 0 (or false) as LastOne except for the last row - when LastOne should be 1.

I realise I can do it immediately after the Insert with an Update, but I wondered if there is some way to do it within the Select statement that is inserted.


"Lastone" relative to what exactly? Without an ORDER BY to relate to, it's meaningless garbage. With an ORDER BY, it's meaningful and also trivial to do.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1418508
Posted Monday, February 11, 2013 11:37 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 5:16 AM
Points: 85, Visits: 352
I don't believe I said,
"will clearly and certainly make a different entry for ONLY the last row inserted."
I was only trying to provided some information in order to help to further the thought process. Not sure why that would be wrong.



______________________________
AJ Mendo | @SQLAJ
Post #1418585
Posted Monday, February 11, 2013 2:42 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,906, Visits: 26,790
SQLAJ (2/11/2013)
I don't believe I said,
"will clearly and certainly make a different entry for ONLY the last row inserted."
I was only trying to provided some information in order to help to further the thought process. Not sure why that would be wrong.


Not a problem. I thought you were on to something and wanted to know how you made a default that would do this.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1418637
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse