Getting an Error on inserting data when using Linked Server Queries - sql-server

UPDATE : The issue was col1 was hiereachyid type and even a select didnt work for it.
Hi all,
I am getting this error -
Objects exposing columns with CLR types are not allowed in distributed queries. Please use a pass-through query to access remote object '"RemoteDb"."dbo"."RemoteTable"'.
I have already setup the linked server [RemoteServer.dev.com].I was trying to perform an bulk insert from a remote table into the current table something like this -
INSERT INTO [CurrentDb].[dbo].[Mytable]
(
col1,
col2
)
SELECT
col1,col2
FROM [RemoteServer.dev.com].[RemoteDb].[dbo].[RemoteTable]
Can anyone please help me out..thanks.

As the error indicates, you need a pass-through query here because of the datatypes. Try this:
INSERT INTO [CurrentDb].[dbo].[Mytable]
(
col1,
col2
)
SELECT col1, col2
FROM OPENQUERY([RemoteServer.dev.com], 'SELECT col1, col2 FROM [RemoteDb].[dbo].[RemoteTable]')

Related

INSERT where not exists in SingleStore

I am trying to perform INSERT WHERE NOT EXISTS in singlestore using spring jpa native quey but I'm getting Sql grammar exception.
I'm using sql5dialect.
Can someone help me with performing the query
Query - INSERT INTO table(col1) VALUES ('colValue') WHERE NOT EXISTS (SELECT 1 from table WHERE col2 = 'coValue'
I have also tried
INSERT INTO table(col1) Select ('colValue') from dual WHERE NOT EXISTS (SELECT 1 from table WHERE col2 = 'coValue'

Invalid Object Name #temp_Table

When I try to execute this code :
insert into myTable
select col1, col2
from #temp_Table
I got this error:
Invalid Object Name #temp_Table
I am writing this code in SAP Business one as a SQL query and the temp table itself is defined already in the same query.
When I only run
select col1, col2
from #temp_Table
it works fine. Only as insert into does not.
Any suggestion what the problem could be?

Insert oracle data based on column position

Im trying to insert oracle data based on a specific order of my columns. This is what i have tried but i get a sql statement not properly ended error.
INSERT INTO owner.users u
(
select column_name
from all_tab_cols
where owner ='owner' AND table_name= 'users' AND virtual_column='NO'
order by 1
)
VALUES ('foo','bar')

Record count of a view and its corresponding table does not match without any joins in view SQL Server

I have a view as below in sql server:
use database2
Go
CREATE VIEW view1
AS
WITH date_cte(datecol)
AS (select getdate())
Select Col1,
Col2,
,....
,[Select datecol from date_cte]
FROM database1.schema1.TABLE
on top a table in different database.
The record count of table as well as view using statement
Select count(1) from database1.schema1.TABLE -- 15487212
Select count(1) from database2.schema2.view1 -- 13324921
Does this problem have any solution?
First of all... you definitely should improve your code:
USE database2
Go
CREATE VIEW schema2.view1
AS
SELECT Col1,
Col2,
,....
,getdate()
FROM database1.schema1.TABLE
Why do you use a CTE just for the date? Another thing, which may cause your error. You create a view without defining your schema. Maybe your view is created but in a different schema? I've added schema2 to your CREATE due to the fact your querying schema2 at the end.
By the way. Your select can be improved too:
Select count(*) from database1.schema1.TABLE -- ??
Select count(*) from database2.schema2.view1 -- ???
Actually the issue here was that data was loading into source table while I was running query to count records.
Therefore the count difference was prevailing.
Thanks for your input

Paging in SQL Server problems

I have searched for paging in SQL Server. I found most of the solution look like that
What is the best way to paginate results in SQL Server
But it don't meet my expectation.
Here is my situation:
I work on JasperReport, for that: to export the report I just need pass the any Select query into the template, it will auto generated out the report
EX : I have a select query like this:
Select * from table A
I don't know any column names in table A. So I can't use
Select ROW_NUMBER() Over (Order By columsName)
And I also don't want it order by any columns.
Anyone can help me do it?
PS: In Oracle , it have rownum very helpful in this case.
Select * from tableA where rownum > 100 and rownum <200
Paging with Oracle
You should use ROW_NUMBER with an ORDER BY - because without an ORDER BY there is no determinism in how rows are returned. You can run the same query three times and get the results back in three different orders. Especially if merry-go-round scans come into play.
So unless you want your report to have the possibility of showing the same rows to users on multiple pages, or some rows never on any page, you need to find a way to order the result set to make it deterministic.
From my opinion, you can use sql query to find out how many columns in a table, and then find out a proper one for ' order by ' to depend on.
The script of how to get out columns of an table refer to : How can I get column names from a table in SQL Server?
Check out this link
http://msdn.microsoft.com/en-us/library/ms186734.aspx
SQL Server has similar function ROW_NUMBER. Though it behaves a bit differently.
SQL Server provides no guarantee of row order unless you have have specified a column in order by clause. I would recommend that you give an order by clause that has unique values.
Thank for all your help. Because of order by are required when paging in MS SQL Server, so I used ResultSetMetaData to get the Columns name and do paging as well.
You can use the below query aswell.
declare #test table(
id int,
value1 varchar(100),
value2 int)
insert into #test values(1,'10/50',50)
insert into #test values(2,'10/60',60)
insert into #test values(3,'10/60',61)
insert into #test values(4,'10/60',10)
insert into #test values(5,'10/60',11)
insert into #test values(6,'10/60',09)
select *
from ( select row_number() over (order by (select 0)) as rownumber,* from #test )test
where test.rownumber<=5

Resources