SQL Server: Bulk Insert to a Linked Server - sql

I have a local database myDB and a server database serverDB that is linked to myDB as [serverDB].serverDB.dbo. I want to upload a 50,000-row table from a .csv file on my computer to the serverDB. I tried this:
bulk insert #temp from 'filename'
insert into [serverDB].serverDB.dbo.tablename select * from #temp
and it takes ages. I found out that the insert into creates connection for each row, so it looks like it's not an option in this case. Then I tried
bulk insert [serverDB].serverDB.dbo.tablename from 'filename'
and I get the error Invalid object name 'tablename' even thought this table exists in the [serverDB].serverDB database. Does anyone know how I can make SQL "see" the table [serverDB].serverDB.dbo.tablename?

Related

Stored Procedure Select data from Server A and Insert data to Server B

I would like to ask about how to write the Stored Procedure Select data from Server A and Insert data to Server B?
1 . Create a linked server using steps at below link
https://www.sqlshack.com/how-to-create-and-configure-a-linked-server-in-sql-server-management-studio/
Create a synonym for the table from the linked server you are going to use if there is only one table otherwise create synonym for that server so you dont have to use the complete name and if the server changes in future you dont have to go and change in every procedure instead you can just change the synonym.
CREATE SYNONYM MySyn
FOR LinkedServerName.DatabaseName.SchemaName;
GO
Use the synonym in your query to fetch the data.
create procedure procedureName
as
begin
insert into destDB.schema.tablename select * from mysyn.sourcetablename
end

How do I update triggers across multiple databases?

I have a query that I can select the databases from the sys.databases with the triggers that I wish to update. From there I can create a cursor. However when I go into the cursor to update my triggers using a dynamic db name #DatabaseExecuteName that is set to MyDatabaseName.dbo I receive the error ''CREATE/ALTER TRIGGER' does not allow specifying the database name as a prefix to the object name.' Because I am in a cursor I am not able to execute a USE MyDatabaseName ... GO, the GO statement is not allowed inside the CURSOR. I have tried SQLCMD MODE :setvar DatabaseName "MyDatabaseName" with USE [$(DatabaseName)]; to try to set the use database. I feel I am very close however my strength is not SQL queries. I could use some assistance on what I am missing.
You can nest EXEC calls so that you can use a USE and then execute a further statement and you don't need to use GO to seperate the batches. This is a complete script to demonstrate the technique:
create database DB1
go
create database DB2
go
use DB2
go
create table T1 (ID int not null)
go
create table T2 (ID int not null)
go
use DB1
go
exec('use DB2; exec(''create trigger T_T on T1 after insert as
insert into T2(ID) select i.ID from inserted i'')');
select DB_NAME()
insert into DB2..T1(ID) values (1),(2);
select * from DB2..T2
Which then shows that this connection is still in the DB1 database, but the trigger was successfully created on the T1 table within the DB2 database.
What you have to watch for is getting your quote-escaping correct.

How to insert value into DB2 from SQL Server using openquery which are not already inserted?

Is there any option to use not exists in openquery from SQL Server and prevent insert of values which already exist in DB2, from table.
I want to insert data from a table located in SQL Server into a table located in an IBM DB2 server, but I want to check if the value already exists or not.
This works for me actually,
if not exists (select * from openquery(puskarkc, 'select * from puskarlib.castatus'))
begin
insert openquery (puskarkc, 'select * from puskarlib.castatus')
select * from castatus
end

How do I put a text file into a SQL Server 2012 field

I have a table call it tbl1 with a field ASCII_file nvarchar(MAX).
I want to put file 'c:xyz.ght' into field ASCII_file using SSMS SQL Insert statement.
Also for existing records is using update the only way to get the files into the field. I tried copy the file but paste did not show up when I tried to paste the file into the field. Is there any easier way than using a SQL update?
Why can't you do something like this, with a simple insert statement?
INSERT INTO tbl1
VALUES ('c:xyz.ght');
And an update statement:
UPDATE tbl1
SET ASCII_file ='c:xyz.ght'
WHERE some_column=some_value;
If you want to read a content of a file:
BULK INSERT tbl1
FROM 'c:\temp\file.txt'
WITH
(
ROWTERMINATOR ='\n'
)

How to execute sql query when debugging a stored procedure

I'm debugging a stored procedure on SQL Server 2008 and I have this:
INSERT INTO #tempTable (ID, Name)
SELECT ID, Name FROM dbo.MYTABLE WHERE dbo.MYTABLE.Old >= 15
How can I view the data into #tempTable on Debug time?
In SQL Server Management Studio, you can't execute query directly while debugging stored procedure, and that's still not implemented(I think). You can only view the local variables value in Local Debug Window.
There are some work around to see temp table values while in Debugging mode:-
1) In the stored procedure, after insert data into #temptable, add this line of code to get temptable values in xml table varriable where you want to see temptable values. Then you can check the values in Local Debug window in xml format
--inserting data into temp table
INSERT INTO #tempTable (ID, Name)
SELECT ID, Name FROM dbo.MYTABLE WHERE dbo.MYTABLE.Old >= 15
--to see records of temp table
DECLARE #temptable XML
SET #temptable = (SELECT * FROM ##temptable FOR XML AUTO)
2) You can convert local temp table(#temptable) to global temptable(##temptable), so when you insert date in temp table, you can open new query window, and able to see global temp table records using select query.
This blog post describes how to access a temporary table from another session:
http://web.archive.org/web/20180409190701/http://sqlblog.com:80/blogs/paul_white/archive/2010/08/14/viewing-another-session-s-temporary-table.aspx
Alternative you can use two ## in the table name to make the table globally accessible from other sessions: ##tempTable (The table might be locked for reading while your insert is running)
Even though SQL Server Management Studio has some debugging functions , but I find them pretty useless.
I don't think there are any debugging tools out there for SQL Server like Visual Studio, which will give you a step by step information at runtime.
The way normally developers debug sql server code is to use print statement, for stored procedures take the sp definition out declare a variable for each parameter that procedure expects , hardcode the values for variables and execute smaller logical blocks of code to see what's going on where.