How to compare two records in SQL Server - sql-server

I have created a stored procedure which joins data from various tables and returns a result set in temporary table.
Now when I update the record from the GUI, I want only those fields in my UI to show up which are changed. And the rest of the columns should remain blank since they were not edited.
How can I make change in my stored procedure such that it returns only the edited fields from second time onwards?

think about using unpivot for each table
and compare the result by identity and fields name
Using PIVOT and UNPIVOT

Related

SQL Server / SSIS: Create new ID for inserted row of merge

I am kind of stuck:
Scenario:
I have a SSIS-Package, which loads data incrementally. In the first step, I load rows from a source which have a) been inserted or b) updated into a staging table. I do this by using the last timestamp of the source table.
In the next step, I am trying to use a MERGE-Statement to update the data in another database (similar to a data warehouse). I have no control over this other database, otherwise my task would be quite easy.
Problem:
The data warehouse table includes an ID-Column ([cId], BIGINT), which it does not set by itself. I have tried to create a sequence, from which I pull a value whenever I insert a new row into the data warehouse (not when I update a row, since that row will already have an ID). However, as specified here, SQL Server will not let me use the next value from my sequence for the target of a MERGE-Statement. Since I have no control over the data warehouse, I cannot change this.
Another solution would be to get the next value from my sequence when I load the data into the staging table. This, however, will result in "holen" in my ID-sequence, because when I update a row in the data warehouse from my staging table, the [cId] column would not be updated, since that row already has an ID.
Does anyone have an idea how to solve this? I am basically trying to pull a new, unique BIGINT, whenever I do an insert inside my MERGE-Statement.
Thanks!

Insert properties of DapperRow into database?

I have a DapperRow dynamic object containing fields retrieved from a table. I had gotten the DapperRow from a previous Query.
I'd like to insert these fields, and a few others, into a different table. However, I can't pass the dynamic in the Execute
con.Execute(insertStatment, rowData);
I will drop back to standard SQL and bind the parameters myself -- I'm just wondering if there's some Dapper feature I'm missing.
Thanks

Update query for data coming from 2 different tables in a single query

I have a table X_Data where some data is saved based on Itemcodes. I have another table Item_Master where the item details are stored. In both tables there is a column called leadtime. But in table X_Data, it gets updated by some stored procedure calculations.
For certain condition, I need this data to be refreshed with the one I have in Item_Master table. I don't want to include any loop or stored procedure for this.
Is it possible to update the data in X_Data table for all items using a single query?

Change tracking -- simplest scenario

I am coding in ASP.NET C# 4. The database is SQL Server 2012.
I have a table that has 2000 rows and 10 columns. I want to load this table in memory and if the table is updated/inserted in any way, I want to refresh the in-memory copy from the DB.
I looked into SQL Server Change Tracking, and while it does what I need, it appears I have to write quite a bit of code to select from the change functions -- more coding than I want to do for a simple scenario that I have.
What is the best (simplest) solution for this problem? Do I go with CacheDependency?
I currently have a similar problem: I'm implementing a rest service that returns a table with 50+ columns and I want to cache the data on the client to reduce trafic.
I'm thinking about this implementation:
All my tables have the fields
ID AutoIncrement (primary key)
Version RowVersion (a numeric value that will be incremented
every time the record is updated)
To calculate a "fingerprint" of the table I use the select
select count(*), max(id), sum(version) from ...
Deleting records changes the first value, inserting the second value and updating the third value.
So if one of the three values changes, i have to reload the table.

Select last updated row ID and last deleted row ID in trigger before they occur in SQL Server

I need to get the id of the updated row in a table to use it to update another table via trigger
Also need to get the id of the deleted row in a table to use it to update another table via trigger
How can I do this?
Is there any built in functions in SQL Server?
If not what kind of trick that can help to accomplish this
Within your trigger, if you want to know the OLD value that was either updated / deleted
SELECT idColumnName FROM deleted
Where idColumnName is the column that contains the ID that you are interested in.
You can then use this ID value to then perform whatever processing that you need.
Additionally. if you want to use the NEW value being updated, the below query gives you that. This is useful especially in case of Updates where you want to compare old / new values of certain fields. In your case, since its an ID column, this will probably not be relevant
SELECT idColumnName FROM inserted

Resources