Update with function for value only seems to evaluate the function once - sql

I'm trying to change all the NULLs in an INT column to an incrementing value using a statement like this:
UPDATE [TableName] SET [ColumnName] = dbo.FunctionName() WHERE [ColumnName] IS NULL
where the function looks like this:
CREATE FUNCTION FunctionName() RETURNS INT AS
BEGIN
RETURN (select MAX(ColumnName) + 1 from TableName)
END
but the result of this is that all the values that were NULL are now set to the same value (i.e. 1 greater than the max before the statement was called).
Is this the expected behaviour? If so, how would I go about getting the result I'm after?
I'd like to avoid using an auto-increment column as all I really want is uniqueness, the incrementing is just a way of getting there.

Yes, this is by design, it is called Halloween protection. To achieve what you want (that is a one time) use an updatable CTE:
with cte as (
SELECT Column,
ROW_NUMBER() OVER () as rn
FROM Table
WHERE Column IS NULL)
UPDATE t
SET t.Column = m.max + t.rn
FROM cte t
JOIN (
SELECT MAX(Column) as max
FROM Table) m;

Related

If else using case for returning value

I am trying to do an if else statement using CASE. If no records, then make it 0 then +1. Or else, just take the last record then +1. At first try i used ISNULL(statement,0). But it doesn't have else statement.
Then I saw many other examples on StackOverflow for a case statement, but I don't seem to understand how to implement it
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM,
)
VALUES (
'2019-01-18 16:59:29',
'KIOSK1',
((SELECT TOP 1 BOOKINGREFERENCENUM FROM TICKET_SALES ORDER BY BOOKINGREFERENCENUM DESC)+1),
);
If you need to use expressions to supply values, you can change VALUES for SELECT, although you will only be able to supply 1 row with SELECT (unless you use UNION ALL).
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM
)
SELECT
'2019-01-18 16:59:29',
'KIOSK1',
1 + ISNULL(
(SELECT TOP 1 BOOKINGREFERENCENUM FROM TICKET_SALES ORDER BY BOOKINGREFERENCENUM DESC),
0)
Please be ware of using this type of "tricks" to generate IDs, as others pointed out, it's a very bad idea (check StepUp's answer to know how to correct).
Just create table with Idetitity(1, 1):
CREATE table TicketSales
(
TrxDate DATETIME
, KioskId INT
, BookingReferenceNum int IDENTITY(1,1)
)
As MSDN says:
Identity columns can be used for generating key values. The identity
property on a column guarantees the following:
Each new value is generated based on the current seed & increment.
Each new value for a particular transaction is different from other
concurrent transactions on the table.
The identity property on a column does not guarantee the following:
Uniqueness of the value - Uniqueness must be enforced by using a
PRIMARY KEY or UNIQUE constraint or UNIQUE index.
... to further read
The easiest way IMO would be to declare variable to hold desired value, assign value to it in easy-to-read way, and then use it in INSERT statement.
Moreover, no need to takke TOP 1 with ordering, just use MAX function :)
DECLARE #id INT;
SELECT #id = CASE COUNT(*) WHEN 0 THEN 0 ELSE MAX(BOOKINGREFERENCENUM) + 1 END
FROM MyTable;
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM,
)
VALUES (
'2019-01-18 16:59:29',
'KIOSK1',
#id
);
NOTE: you are reinventing the wheel, SQL has something like IDENTITY, it's good for this kind of task.

How can I iterate through SQL results like a for loop and an array?

I have a table, and I want to select only the single column of row IDs from it, but in a specific order. Then, I want to loop through that column like below:
for (i=0; i<rows.length; i++)
{
if(i==rows.length-1)
UPDATE myTable SET nextID = NULL WHERE ID = rows[i]
ELSE
UPDATE myTable SET nextID = rows[i+1] WHERE ID = rows[i]
}
I just dont know how to access the results of my select statement with an index like that. Is there a way of doing this in sql server?
Since you didn't provide many details, let's pretend your table looks something like this:
create table MyTable (
Id int not null primary key,
Name varchar(50) not null,
NextId int
)
I want to select only the single column of row IDs from it, but in a specific order
Let's just say that in this case, you decide to order the rows alphabetically by Name. So let's pretend that the select statement that you want to loop through looks like this:
select Id
from MyTable
order by Name
That being the case, instead of looping through the rows and attempting to update each row using the pseudo-code you provided, you can replace the whole thing with a single update statement that will perform the exact same work:
with cte as (
select *,
NewNextId = lead(Id) over (order by Name)
from MyTable
)
update cte
set NextId = NewNextId
Just make sure to adjust the order by clause to whatever your specific order really is. I just used Name in my example, but it might be something else in your case.
You could use a cursor, or you could use something a bit smarter.
Your example should be able to be written fairly easily along the lines of:
update mytable set nextID = LEAD(id,1) over (order by id)
Lead(id,1) will grab the next id, 1 row ahead in the record set and update the nextID field with it. If it can't find one it will return null. No looping or conditional logic needed!
edit: I forgot the over clause. This is the part that tells it how you would like it ordered for the lead

How the change the cte column value

I want to get the CTE each column value stored in a variable then perform some operation on it. At last stored variable values into other table. But there are more than 10 records in a CTE so I am confused how i do this?
Declare #LineRead nvarchar(max)
;with cte(ID,RecordLine) as (
select Id,RecordLine from [dbo].[WorkDataImport]
)
select #LineRead = RecordLine + 'TEmp'
print LineRead
Result is
xyzaaddda Temp
I dont know why i get only one records.
That's because you are using SELECT for variable assignation.
SQL Server supports nonstandard assignment SELECT statement, which allows querying
data and assign multiple values obtained from the same row to multiple variables by using a single statement.
The assignment SELECT has predictable behavior when exactly one row qualifies. However, if the query has more than one qualifying row, the code doesn’t fail. The assignments take place per each qualifying row, and with each row accessed, the values from the current row overwrite the existing values in the variables. When the assignment SELECT finishes, the values in the variables are those from the last row that SQL Server happened to access.
That's why you are getting only one row.
Replace the SELECT with SET and the code will throw error as:
SET #LineRead = RecordLine + 'TEmp'
One way is to save all the rows from CTE to temp table and then perform the manipulations as:
;with cte(ID,RecordLine) as (
select Id,RecordLine from [dbo].[WorkDataImport]
)
select RecordLine + 'TEmp' as LineRead
into #Temp1
from cte
select * from #Temp1
Demo
Try as below:
;with mycte(ID,RecordLine) as (
select Id,RecordLine from [dbo].[WorkDataImport]
)
select RecordLine + 'TEmp' into #temp from mycte
THEN retrieve all the records from #temp(temp table)
select * from #temp

SQL Server random using seed

I want to add a column to my table with a random number using seed.
If I use RAND:
select *, RAND(5) as random_id from myTable
I get an equal value(0.943597390424144 for example) for all the rows, in the random_id column. I want this value to be different for every row - and that for every time I will pass it 0.5 value(for example), it would be the same values again(as seed should work...).
How can I do this?
(
For example, in PostrgreSql I can write
SELECT setseed(0.5);
SELECT t.* , random() as random_id
FROM myTable t
And I will get different values in each row.
)
Edit:
After I saw the comments here, I have managed to work this out somehow - but it's not efficient at all.
If someone has an idea how to improve it - it will be great. If not - I will have to find another way.
I used the basic idea of the example in here.
Creating a temporary table with blank seed value:
select * into t_myTable from (
select t.*, -1.00000000000000000 as seed
from myTable t
) as temp
Adding a random number for each seed value, one row at a time(this is the bad part...):
USE CPatterns;
GO
DECLARE #seed float;
DECLARE #id int;
DECLARE VIEW_CURSOR CURSOR FOR
select id
from t_myTable t;
OPEN VIEW_CURSOR;
FETCH NEXT FROM VIEW_CURSOR
into #id;
set #seed = RAND(5);
WHILE ##FETCH_STATUS = 0
BEGIN
set #seed = RAND();
update t_myTable set seed = #seed where id = #id
FETCH NEXT FROM VIEW_CURSOR
into #id;
END;
CLOSE VIEW_CURSOR;
DEALLOCATE VIEW_CURSOR;
GO
Creating the view using the seed value and ordering by it
create view my_view AS
select row_number() OVER (ORDER BY seed, id) AS source_id ,t.*
from t_myTable t
I think the simplest way to get a repeatable random id in a table is to use row_number() or a fixed id on each row. Let me assume that you have a column called id with a different value on each row.
The idea is just to use this as a seed:
select rand(id*1), as random_id
from mytable;
Note that the seed for the id is an integer and not a floating point number. If you wanted a floating point seed, you could do something with checksum():
select rand(checksum(id*0.5)) as random_id
. . .
If you are doing this for sampling (where you will say random_id < 0.1 for a 10% sample for instance, then I often use modulo arithmetic on row_number():
with t as (
select t.* row_number() over (order by id) as seqnum
from mytable t
)
select *
from t
where ((seqnum * 17 + 71) % 101) < 0.1
This returns about 10% of the numbers (okay, really 10/101). And you can adjust the sample by fiddling with the constants.
Someone sugested a similar query using newid() but I'm giving you the solution that works for me.
There's a workaround that involves newid() instead of rand, but it gives you the same result. You can execute it individually or as a column in a column. It will result in a random value per row rather than the same value for every row in the select statement.
If you need a random number from 0 - N, just change 100 for the desired number.
SELECT TOP 10 [Flag forca]
,1+ABS(CHECKSUM(NEWID())) % 100 AS RANDOM_NEWID
,RAND() AS RANDOM_RAND
FROM PAGSEGURO_WORK.dbo.jobSTM248_tmp_leitores_iso
So, in case it would someone someday, here's what I eventually did.
I'm generating the random seeded values in the server side(Java in my case), and then create a table with two columns: the id and the generated random_id.
Now I create the view as an inner join between the table and the original data.
The generated SQL looks something like that:
CREATE TABLE SEED_DATA(source_id INT PRIMARY KEY, random_id float NOT NULL);
select Rand(5);
insert into SEED_DATA values(1,Rand());
insert into SEED_DATA values(2, Rand());
insert into SEED_DATA values(3, Rand());
.
.
.
insert into SEED_DATA values(1000000, Rand());
and
CREATE VIEW DATA_VIEW
as
SELECT row_number() OVER (ORDER BY random_id, id) AS source_id,column1,column2,...
FROM
( select * from SEED_DATA tmp
inner join my_table i on tmp.source_id = i.id) TEMP
In addition, I create the random numbers in batches, 10,000 or so in each batch(may be higher), so it will not weigh heavily on the server side, and for each batch I insert it to the table in a separate execution.
All of that because I couldn't find a good way to do what I want purely in SQL. Updating row after row is really not efficient.
My own conclusion from this story is that SQL Server is sometimes really annoying...
You could convert a random number from the seed:
rand(row_number over (order by ___, ___,___))
Then cast that as a varchar
, Then use the last 3 characters as another seed.
That would give you a nice random value:
rand(right(cast(rand(row_number() over(x,y,x)) as varchar(15)), 3)

Sql query that numerates the returned result

How to write one SQL query that selects a column from a table but returns two columns where the additional one contains an index of the row (a new one, starting with 1 to n). It must be without using functions that do that (like row_number()).
Any ideas?
Edit: it must be a one-select query
You can do this on any database:
SELECT (SELECT COUNT (1) FROM field_company fc2
WHERE fc2.field_company_id <= fc.field_company_id) AS row_num,
fc.field_company_name
FROM field_company fc
SET NOCOUNT ON
DECLARE #item_table TABLE
(
row_num INT IDENTITY(1, 1) NOT NULL PRIMARY KEY, --THE IDENTITY STATEMENT IS IMPORTANT!
field_company_name VARCHAR(255)
)
INSERT INTO #item_table
SELECT field_company_name FROM field_company
SELECT * FROM #item_table
if you are using Oracle or a database that supports Sequence objects, make a new db sequence object for this purpose. Next create a view, and run this.
insert into the view as select column_name, sequence.next from table
In mysql you can :
SELECT Row,Column1
FROM (SELECT #row := #row + 1 AS Row, Column1 FROM table1 )
As derived1
I figured out a hackish way to do this that I'm a bit ashamed of. On Postgres 8.1:
SELECT generate_series, (SELECT username FROM users LIMIT 1 OFFSET generate_series) FROM generate_series(0,(SELECT count(*) - 1 FROM users));
I believe this technique will work even if your source table does not have unique ids or identifiers.
On SQL Server 2005 and higher, you can use OVER to accomplish this:
SELECT rank() over (order by company_id) as rownum
, company_name
FROM company