(Select * from table) from another pc without linked server - sql-server

I am trying to find if there is any way, selecting table from another pc, 'without' creating a linked server.
I found an example which i setting up my sql connection string inside my query, but it gives me error that i need to create a linked server.
SELECT * FROM OPENDATASOURCE (
'SQLNCLI'
,'Data Source=192.168.0.3,1433;Initial Catalog=wiorder;User ID=admin;Password=1234').inventorymaster
Solved by Dan.
When i am trying to insert from destination pc to local pc i am getting error:
Msg 8114, Level 16, State 5, Line 4
Msg 8101, Level 16, State 1, Line 3
An explicit value for the identity column in table 'InventoryMaster' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Here is my query
SET IDENTITY_INSERT InventoryMaster ON
insert into InventoryMaster
SELECT * FROM OPENDATASOURCE (
'SQLNCLI'
,'Data Source=192.168.0.3,1433;Initial Catalog=WiOrder;User ID=admin;Password=1234').WiOrder.dbo.inventorymaster
SET IDENTITY_INSERT InventoryMaster OFF

Related

How do I select from a linked temp table, into a local temp table?

I'm currently calling a stored procedure on a linked server. This stored procedure selects data into a temporary table on this linked server. I'm then attempting to select this data into a temporary table on the local server, so that I can manipulate the data and pull it into various tables.
If I manually run the stored procedure on the server, I'm able to run the second part of the query (--SELECT DATA FROM TEMP TABLE). However, although I'm able to successfully call the stored procedure via the first part of my query, when it gets to the second part, I get the below errors:
Msg 8180, Level 16, State 1, Line 66
Statement(s) could not be prepared.
Msg 208, Level 16, State 1, Line 66
Invalid object name '##Sales'.
Is there another method I could use here? SSIS isn't an option right now, the requirement is to code this via T-SQL.
--CALL STORED PROC FOR SALES DATA
DECLARE #RunSalesStoredProcSQL VARCHAR(1000);
SET #RunSalesStoredProcSQL = 'EXEC [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]';
EXEC (RunSalesStoredProcSQL) AT [SERVER\INSTANCE];
Print ‘Sales Procedure Executed';
--SELECT DATA FROM TEMP TABLE
SELECT *
INTO ##TempTable
FROM OPENQUERY([SERVER\INSTANCE], 'SELECT * FROM dbo.##Sales');
Print 'Data Selected';
You could return the data from linked server procedure as a simple select, not selecting it in a temp table, and then you can do the following on the local server:
INSERT INTO ##TempTable(ColNames)
EXECUTE [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]
--select data
SELECT * FROM ##TempTable

Export temp table data or stored procedure to excel using openrowset

I have a procedure within that I have created global temp table and normally 'select from temptable' giving output. When I use that in the openrowset command to export to excel getting error
Msg 7399, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7303, Level 16, State 1, Line 2
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
First I tried with direct stored procedure only but same error
then with local temp table also same error
then with global temp table its giving same error
When I use normal query I will get the excel file. My query is like bellow
exec Proc_CardexSystem #periodical_id=150,#periodicity='',#from_year=2014,#to_year=2018
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database="C:/Users/Mahantesh/Desktop/cardex.xlsx;',
'select * from [CurrentCirculationList$]')select * from ##cte2_results
within procedure I have created temp table as bellow format and the procedure contains 'cte' also
if OBJECT_ID('tempdb..##othertemp_table') is not null
drop table ##othertemp_table
if OBJECT_ID('tempdb..##temp_table ') is not null
drop table ##temp_table
select columns into ##temp_table from ##othertemp_table

Migrate data from non Normalized database to a well Design database

I am migrating reads from a poorly design table to the newly Normalize one in sqlserver 2008.
My goal here is to get a count of total record in the Source table store in in a variable. The loop through each row and insert to the destination table. I have made numerous search on similar posts:
1: How do I insert a record from one table into another table? and Inserting into database by reading another
But was able to modify a code from SQL Examples
Now am get this error:
Msg 207, Level 16, State 1, Line 5
Invalid column name 'Num_Row_in_table
Code:
DECLARE #i INT
DECLARE #Num_Row_in_table int
SET #i=1
WHILE(#i <= Num_Row_in_table)
BEGIN
Insert into Destinationtable (FirstName,LastName,Photo,SN)
(
Select FirstName,LASTNAME,Photo,SN From MyDB.dbo.sourcetable
)
SET #i=#i+1
END
Forget the cursor; just do this:
Insert into Destinationtable (FirstName,LastName,Photo,SN)
Select FirstName,LASTNAME,Photo,SN From MyDB.dbo.sourcetable

Inserting multiple values into a temporary table, SQL Server

I am using Microsoft SQL Server Management Studio, I am trying to run the following query to input values into a temporary table to use later:
CREATE TABLE #temptable
(colnumber varchar(15), dispcode varchar(10))
INSERT INTO #temptable (colnumber, dispcode)
VALUES
('col5', '811'),
('col6', '817'),
('col7', '823'),
('col8', '825');
When running I get the following error:
Msg 102, Level 15, State 1, Line 50
Incorrect syntax near ','.
Which points to the line "('col5', '811'),"
Could anyone help me identify the problem here?
For SQL Server version <2008 use this:
INSERT INTO #temptable (colnumber, dispcode)
SELECT 'col5', '811'
UNION ALL SELECT 'col6', '817'
UNION ALL SELECT 'col7', '823'
UNION ALL SELECT 'col8', '825'

Cross-database trigger in SQL Server

I have two tables CrossDBTrigTest_1 and CrossDBTrigTest_2 on same SQL Server instance.
The databases both have a table called Employee.
I wrote the following trigger on the Employee table CrossDBTrigTest_1 db:
Create Trigger [dbo].[CrossDBInsert] on [dbo].[employee] after insert
AS
Begin
Set nocount on
Insert into CrossDBTrigTest_2.employee(FirstName, LastName, Date)
SELECT inserted.FirstName, inserted.LastName, getdate()
FROM inserted
End
but the Insert statement fails with message:
Msg 208, Level 16, State 1, Procedure CrossDBInsert, Line 5
Invalid object name 'CrossDBTrigTest_2.employee'.
How do I enable cross database triggers in situations like this??
Shouldn't
CrossDBTrigTest_2.employee(FirstName,LastName,Date)
be
CrossDBTrigTest_2.dbo.employee(FirstName,LastName,Date)
???
Use
CrossDBTrigTest_2..employee
as table name. Note two dots instead of one.

Resources