SSIS: can not run an sql task to update table - sql-server

I'm new to SSIS and SQL Server and what I'm trying to do is to update a table that I have in my database. But it doesn't work with "Execute SQL Task".
Here is what I'm trying to do:
Create 2 tables if they do not exists (one the final table and the other to store the data that already exists in the database)
get the data from excel file and my database
then I use sort and merge join + derived column + conditional split to get the newest data into the final table and the data that already exists in the database in another table
afterwards, I'm trying to update the table but here is the problem
Here is the SQL query I use:
update
A
set
A.[nom technicien] = B.[nom technicien],
A.[nb intervention] = B.[nb intervention],
A.[tel] = B.[tel],
A.[mail] = B.[mail],
A.[adresse] = B.[adresse],
A.[id] = B.[id]
from
fichetech A
inner join fichetechstaged B on
A.[id] = B.[id];
I searched on the internet and haven't found anything that would help me.
Here is a screenshot of the error I get:

Well i figure it out by myself xD. It took me a while but i did it.
So, what worked for me is: in SQL EXECUTE TASK-> SQL STATEMENT -> BypassPrepare -> False and it worked perfectly.
IDK why it work. If someone can explain it would be great.
Thanks everyone for the help.

Related

SQL Delete using select on linked server issue

I have 2 databases with the same tables.
Both databases are on different SQL servers.
I added the second SQL server as a linked server, which works fine.
I want to run a simple DELETE on the linked DB (so that ID's that aren't on the local DB will be deleted).
When I have both DB's on the same server, it works
DELETE FROM TM.dbo.Departments
WHERE NOT EXISTS (SELECT * FROM SPO.dbo.Departments
WHERE TM.dbo.Departments.DepartmentID = spo.dbo.Departments.DepartmentID);
But when I try this on the Linked Server, it looks like this
DELETE FROM [LINKEDSRV].[TM].[dbo].[Departments]
WHERE NOT EXISTS (SELECT * FROM SPO.dbo.Departments WHERE
spo.dbo.Departments.DepartmentID = [LINKEDSRV].[TM].[dbo].[Departments].DepartmentID)
And the last line with is where I can't get it to work.
I hope you guys have a suggestion!
Try this:
DELETE LS
FROM [LINKEDSRV].[TM].[dbo].[Departments] LS
WHERE NOT EXISTS (SELECT 1 FROM SPO.dbo.Departments D
WHERE D.DepartmentID = LS.DepartmentID)
As I mentioned in the comments, 3+ naming for columns is deprecated (i.e. schema.object.column). Alias your objects and then prefix the column name with that for succinct and readable SQL.

SQL Server Linked Server Update - terrible performance

In my SQL Server 2012 database, I have a linked server reference to a second SQL Server database that I need to pull records from and update accordingly.
I have the following update statement that I am trying to run:
UPDATE
Linked_Tbl
SET
Transferred = 1
FROM
MyLinkedServer.dbo.MyTable Linked_Tbl
JOIN
MyTable Local_Tbl ON Local_Tbl.LinkedId = Linked_Tbl.Id
JOIN
MyOtherTable Local_Tbl2 ON Local_Tbl.LocalId = Local_Tbl2.LocalId
Which I had to stop after an hour of running as it was still executing.
I've read online and found solutions stating that the best solution is to create a stored procedure on the Linked Server itself to execute the update statement rather than run it over the wire.
The problems I have are:
I don't have the ability to create any procedures on the other server.
Even if I could create that procedure, I would need to pass through all the Ids to the stored procedure for the update and I'm not sure how to do that efficiently with thousands of Ids (this, obviously, is the smaller of the issues, though since I can't create that procedure in the first place).
I'm hoping there are other solutions people may have managed to come up with given that it's often the case you don't have permissions to make changes to a different server.
Any ideas??
I am not sure, whether it can give more performance, you an try:
UPDATE
Linked_Tbl
SET
Transferred = 1
FROM OPENDATASOURCE([MyLinkedServer],'select Id, LocalId,Transferred from remotedb.dbo.MyTable') AS Linked_Tbl
JOIN MyTable Local_Tbl
ON Local_Tbl.LinkedId = Linked_Tbl.Id
JOIN MyOtherTable Local_Tbl2
ON Local_Tbl.LocalId = Local_Tbl2.LocalId

How can I get data from 2 different SQL Servers

I have the following situation. I am working with 2 separate SQL servers. Server A hosts the company HR data. There is a view on Server a that provides supervisor info for each employee. I need to get the next supervisor info going up the chain. So I used this to code, I got from the DB admin, to accomplish that
SELECT *
FROM [lawdata].[dbo].[All_Users] ru1
left outer join [lawdata].[dbo].[All_Users] ru2 on ru1.SUPER_EID = ru2.EMP_EID
Now I have data on a separate SQL Server, Server B, that contains some report data the ReportData table contains the employee ID which matches employee ID numbers shown in the view above from Server A. The questions is how can I merge the view from Server A and the Employee ID on Server B so I can link the supervisors to the data rows on Server B.
I have seen this post but just cannot get the syntax right to make it work with my situation
Thanks
You need linked servers. then use
[ServerName].[DatabaseName].[dbo].[tableName]
Create Linked Servers (SQL Server Database Engine)
For this, I'd create an SSIS package to pull down the data from the lawdata server into the database on Server B once a night - probably just a truncate and reload. This way, all of your queries with lawdata data on Server B is localized to one database on one server.
it looks like in your code you did a left outer join on something with itself. Try
SELECT *
FROM [server1].[dbname].[dbo].[tablename] A
left outer join [server2].[dbname].[dbo].[tablename] B on A.columnname = B.columnname
where ["insert where clause here"]
Just in case someone else is trying to solve this same problem here is the solution I came up with; thanks to the suggestion given above
select rd.*, ru1.emp_first, ru1.emp_last, ru1.Super_Last as FirstLineLast,
Super_first as FirstLineFirst,
ru2.Super_Last as SecondLineLast,
2.Super_first as SecondLineFirst
from [TaserEvidence].[dbo].[ReportData] rd left outer join
[soops-lawrept].[lawdata].[dbo].[My_View] ru1 on rd.OwnerBadgeId = ru1.emp_EID
left outer join
[soops-lawrept].[lawdata].[dbo].[rob_users] ru2 on ru1.super_EID = ru2.EMP_EID

Verifying Syntax of a Massive SQL Update Command

I'm new to SQL Server and am doing some cleanup of our transaction database. However, to accomplish the last step, I need to update a column in one table of one database with the value from another column in another table from another database.
I found a SQL update code snippet and re-wrote it for our own needs but would love someone to give it a once over before I hit the execute button since the update will literally affect hundreds of thousands of entries.
So here are the two databases:
Database 1: Movement
Table 1: ItemMovement
Column 1: LongDescription (datatype: text / up to 40 char)
Database 2: Item
Table 2: ItemRecord
Column 2: Description (datatype: text / up to 20 char)
Goal: set Column1 from db1 to the value of Colum2 from db2.
Here is the code snippet:
update table1
set table1.longdescription = table2.description
from movement..itemmovement as table1
inner join item..itemrecord as table2 on table1.itemcode = table2.itemcode
where table1.longdescription <> table2.description
I added the last "where" line to prevent SQL from updating the column where it already matches the source table.
This should execute faster and just update the columns that have garbage. But as it stands, does this look like it will run? And lastly, is it a straightforward process, using SQL Server 2005 Express to just backup the entire Movement db before I execute? And if it messes up, just restore it?
Alternatively, is it even necessary to re-cast the tables as table1 and table 2? Is it valid to execute a SQL query like this:
update movement..itemmovement
set itemmovement.longdescription = itemrecord.description
from movement..itemmovement
inner join item..itemrecord on itemmovement.itemcode = itemrecord.itemcode
where itemmovement.longdescription <> itemrecord.description
Many thanks in advance!
You don't necessarily need to alias your tables but I recommend you do for faster typing and reduce the chances of making a typo.
update m
set m.longdescription = i.description
from movement..itemmovement as m
inner join item..itemrecord as i on m.itemcode = i.itemcode
where m.longdescription <> i.description
In the above query I have shortened the alias using m for itemmovement and i for itemrecord.
When a large number of records are to be updated and there's question whether it would succeed or not, always make a copy in a test database (residing on a test server) and try it out over there. In this case, one of the safest bet would be to create a new field first and call it longdescription_text. You can make it with SQL Server Management Studio Express (SSMS) or using the command below:
use movement;
alter table itemmovement add column longdescription_test varchar(100);
The syntax here says alter table itemmovement and add a new column called longdescription_test with datatype of varchar(100). If you create a new column using SSMS, in the background, SSMS will run the same alter table statement to create a new column.
You can then execute
update m
set m.longdescription_test = i.description
from movement..itemmovement as m
inner join item..itemrecord as i on m.itemcode = i.itemcode
where m.longdescription <> i.description
Check data in longdescription_test randomly. You can actually do a spot check faster by running:
select * from movement..itemmovement
where longdescription <> longdescription_test
and longdescription_test is not null
If information in longdescription_test looks good, you can change your update statement to set m.longdescription = i.description and run the query again.
It is easier to just create a copy of your itemmovement table before you do the update. To make a copy, you can just do:
use movement;
select * into itemmovement_backup from itemmovement;
If update does not succeed as desired, you can truncate itemmovement and copy data back from itemmovement_backup.
Zedfoxus provided a GREAT explanation on this and I appreciate it. It is excellent reference for next time around. After reading over some syntax examples, I was confident enough in being able to run the second SQL update query that I have in my OP. Luckily, the data here is not necessarily "live" so at low risk to damage anything, even during operating hours. Given the nature of the data, the updated executed perfectly, updating all 345,000 entries!

Perform checksum to check if source table rows are loaded in target table

I'm trying to load data from old SQL Server 2000 to new SQL Server 2014. I need to do a checksum to check if all the source data is loaded in the target database (SQL Server 2014).
I've created the insert statement for the same which works. I need to use checksum to make sure all the source rows are loaded in the target table. Can somebody give me a hand on this?
Any help is appreciated.
Here is my insert statement:
INSERT INTO [Test].[dbo].[Order_tab]
([rec_id]
,[date_loaded]
,[Name1]
,[Name2]
,[Address1]
,[Address2]
,[City]
,[State]
,[Zipcode]
,[e_Name1])
SELECT s.[rec_id]
,s.[date_loaded]
,s.[Name1]
,s.[Name2]
,s.[Address1]
,s.[Address2]
,s.[City]
,s.[State]
,s.[Zipcode]
,ENCRYPTBYKEY(key_guid('EncryptionKey'),s.[Name1])
FROM [LinkedServer].[SourceTest].[dbo].[Order_tab] s
left join [Test].[dbo].[Order_tab] d on d.rec_id= s.rec_id
where d.rec_id IS NULL
Hi this is a very naiv solution, but if you only need a one time check, it is fast :)
SELECT SUM([rec_id])
FROM [Test].[dbo].[Order_tab]
And then compare it to:
SELECT SUM([rec_id])
FROM [LinkedServer].[SourceTest].[dbo].[Order_tab]
If you got everything over the SUM of the Id's should be the same :)

Resources