SQL - NEWID() producing the same guid on an update query - sql

In a table I have created a new column that's going to hold a randomized alphanumeric string. I've been using the NEWID() function. From my understanding, NEWID should just create a new guid per row but all my rows end up with the same guid.
My code is:
DECLARE #random_guid
SET #random_guid = NEWID()
UPDATE this_table
SET random_column = #random_guid
Thanks in advance for any help!

That's probably cause you are setting the variable first and using it. Rather directly use the function like
UPDATE this_table
SET random_column = NEWID();

Related

How do I create a simple index column in a table in SQL Server?

I've been having trouble figuring out how to add a simple integer-index column to an existing table in a SQL Server database. The closest question I've been able to find on StackOverflow isn't as simple as my question.
I'm trying to figure out how to turn this:
into an actual column.
I've seen this:
CREATE INDEX idx_SomeColumn
ON SomeTable (SomeColumn);
but I do not understand the significance of the SomeColumn reference and the database is shared so I don't have much leeway to test.
First of all you need new column populated with id.
DECLARE #id INT
SET #id = 0
UPDATE YourTable
SET #id = ID_COL = #id + 1
GO
then you need to create index on it
CREATE INDEX idx_Id_Col
ON YourTable (Id_Col);

MS SQL use IDENT_CURRENT as variable of query

I need to use the IDENT_CURRENT of a specific table inside a query I'm writing.
I don't want to do a query to store the ID and another query to use it. I'd like something like:
UPDATE my_table1
SET my_column1 = IDENT_CURRENT(my_table2)
WHERE my_column2 = ?
Is it possible?
Yes, this code will work fine.
Table name for the function IDENT_CURRENT must be in quotas:
UPDATE my_table1
SET my_column1 = IDENT_CURRENT('my_table2')
WHERE my_column2 = ?
Needs quotes around table name.
UPDATE my_table1
SET my_column1 = IDENT_CURRENT('my_table2')
WHERE my_column2 = ?

How to declare variable in sql update statement

how to update with a new variable
let's say I wanted to do the following
update T
set T.property1 = (declare #temp varch(20)
#temp = 'testing')
from #temp_table_name T
is this possible. I need to update a table but the new element is the end result of a series of complicated statements and it would be a lot easier to define some variables along the way to handle intermediate outputs. What is the correct syntax for what I'm trying to do above because it's not working
Is something like this what you're looking for?
DECLARE #temp varchar(20)
SET #temp = 'testing, or the result of a query maybe?'
UPDATE T SET T.property1 = #temp
FROM #temp_table_name T
WHERE 1 = 1
Move all of those statements into a scalar valued user-defined function and then in your update statement do this:
update T
set T.property1 = dbo.myUdf(...)
from #temp_table_name T
where ... are any parameters it may need from the row to do its job.

How can I have a temporary variable declared in a MERGE statement in SQL Server 2008?

I need to use a temporary variable declared in stored procedure. I need to use this variable to assign a value and do some function in a Matched statement. How can I use? is there any other way to have value??
Thanks in advance
This is how you can define a local variable in SQL Server:
DECLARE #MyVariable INT
SET #MyVariable = 12
SELECT HouseNumber + #MyVariable as NewHouseNumber FROM MyTable WHERE Id = 1
If you declare the variable preceding the MERGE statement then you can indeed use that variable within the MERGE statement. This apples to table variables as well as scalar variables.
I think he ment something like this
MERGE TargetTable as tar
USING SourceTable as src
ON tar.SomeID = src.OtherID
DECLARE #BossId INT
SET #BossId = (SELECT ID FROM EmployeeTable WHERE [BossID] = src.BossID)
--Here we take dynamicly an ID from another table
WHEN NOT MATCHED THEN
INSERT (list OF fields, [BossID])
VALUES (list OF values, #BossId)
WHEN MATCHED THEN
UPDATE
SET (list OF SET statements);
This way in every INSERT statement will have different BossID. Is this even possible? If not - How to insert records this way? Imagine that the SourceTable (which in my case is an input parameter in a SP) came with ID which needs to be mapped with another talbe. Any suggestions?
My post is more like an addition to the original question.

SQL Trigger to update row

I need a SQL trigger that would zero pad a cell whenever its inserted or updated. Was curious if its best practice to append two strings together like I'm doing in the update command. Is this be best way to do it?
CREATE TRIGGER PadColumnTenCharsInserted ON Table
AFTER INSERT
AS
DECLARE
#pad_characters VARCHAR(10),
#target_column NVARCHAR(255)
SET #pad_characters = '0000000000'
SET #target_column = 'IndexField1'
IF UPDATE(IndexField1)
BEGIN
UPDATE Table
SET IndexField1 = RIGHT(#pad_characters + IndexField1, 10)
END
GO
Your padding code looks fine.
Instead of updating every row in the table like this:
UPDATE Table
update just the row that triggered the trigger:
UPDATE updated
Also, you've still got some extraneous code -- everything involving #target_column. And it looks like you're not sure if this is an INSERT trigger or an UPDATE trigger. I see AFTER INSERT and IF UPDATE.
Two questions:
What are you doing with #target_column? You declare it and set it with a column name, but then you never use it. If you intend to use the variable in your subsequent SQL statements, you may need to wrap the statements in an EXECUTE() or use sp_executesql().
The syntax "UPDATE Table..." is OK for your update statement assuming that "Table" is the name of the table you are updating. What seems to be missing is a filter of some kind. Or did you really intend for that column to be updated for every row in the whole table?
One way to handle this would be to declare another variable and set it with the PK of the row that is updated, then use a where clause to limit the update to just that row. Something like this:
DECLARE #id int
SELECT #id = Record_ID FROM INSERTED
-- body of your trigger here
WHERE Record_ID = #id
I like your padding code. It looks good to me.