SQL JOIN AS400 from table #temp - sql-server

I want to make a dynamic join between sql and as400, of this type:
SELECT * FROM OPENQUERY(AS400_link,'SELECT
AS400.CAMPO1
,AS400.CAMPO2
,AS400.CAMPO3
,AS400.CAMPO4
FROM AS400_FILE AS AS400
INNERT JOIN #TAB_TEMP AS TEMP ON
TEMP.CAMPO1 = AS400.CAMPO1
')
DROP TABLE #TAB_TEMP
if I run it:
OLE DB provider "IBMDASQL" for linked server "AS400_link" returned message "SQL0113: Nome #TAB_TEMP non consentito.
Causa . . . : #TAB_TEMP...
some solution?
Thank you

I think what you're looking for is more like
SELECT * FROM OPENQUERY(AS400_link,'SELECT
AS400.CAMPO1
,AS400.CAMPO2
,AS400.CAMPO3
,AS400.CAMPO4
FROM AS400_FILE') AS AS400
INNER JOIN #TAB_TEMP AS TEMP ON
TEMP.CAMPO1 = AS400.CAMPO1
Don't expect it to perform well.

Related

SQL Incorrect syntax: CREATE VIEW must be the only statement in the batch

I'm struggling with a problem in SQL - I keep getting this error:
Incorrect syntax: CREATE VIEW must be the only statement in the batch
I'm using newest Microsoft SQL Server Management Studio.
CREATE VIEW [name]
AS
SELECT
Dostawcy.Nazwa_Firmy,
Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko,
Towary.Nazwa,
SUM(Towary_Dostawy.Ilosc) AS Suma
FROM
Dostawcy
INNER JOIN
Dostawy ON Dostawcy.Nazwa_Firmy = Dostawy.Firma_Dostarczajaca
INNER JOIN
Towary_Dostawy ON Dostawy.ID_Dostawy = Towary_Dostawy.ID_Dostawy
INNER JOIN
Towary ON Towary_Dostawy.ID_Towaru = Towary.ID_Towaru
WHERE
Towary_Dostawy.ID_Towaru = 4
GROUP BY
Towary_Dostawy.ID_Towaru, Towary.Nazwa,
Dostawcy.Nazwa_Firmy, Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko
ORDER BY
Suma DESC
SELECT TOP 1 *
FROM name
I checked if the CREATE VIEW was at the beginning of the code, tried something with GO, but maybe wrong.
Add a go to the line above select top 1 * from [name]
Edit: also, the order by should be removed as it has no effect inside of a view.

ODBC call failed. The multipart identifier "(tablename)" could not be bound. (#4104)

This query doesnt work when i run this query with tables from sql server as linked back end. But works just fine when tables are from linked ms access back end.
I think it has something to do with the join clause.
SELECT qry_Product_Warehouse.Warehouse_ID, qry_Product_Warehouse.Product_ID,
CDbl(Nz(IIf(IsNull([tbl_Product_Quantity].[Stock_Recount_Date]),
Sum([qry_Product_Transaction].[qty]),
[tbl_Product_Quantity].[Stock_Recount_Quantity]+Sum(IIf([qry_Product_Transaction].[Date]>[tbl_product_quantity].[Stock_Recount_Date],[qry_Product_Transaction].[Qty],0))),0)) AS Current_Qty
FROM (qry_Product_Warehouse
LEFT JOIN qry_Product_Transaction ON (qry_Product_Warehouse.Warehouse_ID = qry_Product_Transaction.Location)
AND (qry_Product_Warehouse.Product_ID = qry_Product_Transaction.Product))
LEFT JOIN tbl_Product_Quantity ON (qry_Product_Warehouse.Product_ID = tbl_Product_Quantity.Product)
AND (qry_Product_Warehouse.Warehouse_ID = tbl_Product_Quantity.Warehouse)
GROUP BY qry_Product_Warehouse.Warehouse_ID, qry_Product_Warehouse.Product_ID, tbl_Product_Quantity.Stock_Recount_Quantity, tbl_Product_Quantity.Stock_Recount_Date;

How to add and delete linkedserver in SQL Server in runtime using Vb.net

I use update and insert query using joint tables in two different SQL Servers using linked server. But I don't like my remote server link open to users in SQL Server Management Studio. Instead I would like to create an delete the linked server at runtime. Is this possible using VB.net?
UPDATE Category
SET Category.HQID = B.[ID],
Category.DepartmentID = B.[departmentid],
Category.Name = B.[name],
Category.Code = B.[code],
Category.LastUpdated = B.[lastupdated]
FROM
Category A
INNER JOIN
[Myremoteserver].[MyRemoteDatabase].[dbo].[Category] B ON A.HQID = B.ID
WHERE
A.HQID <> B.ID
OR A.DepartmentID <> B.departmentID
OR A.Name <> B.name
OR A.Code <> B.code
OR A.LastUpdated <> B.lastupdated
Thanks in advance

microsoft sql select query from multiple tables

This query is working in mysql but is not working in microsoft sql server management studio 2008, can someone help me out?
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR,
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id
I am getting the error: Invalid object name cms_question_report
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id
I think this should do
specify Normally it happens when you have specific schema and you don't specify it for example:
Replace dbo. with your schema and/or type your database name
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM databasename.dbo.cms_question_report QR,
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id

Alternative to SQL Server's sysobjects in PostgreSQL

I am porting a database from SQL Server to PostgreSQL. Is there any alternative to sysobjects in PostgreSQL? If so, then what is it and how can be it used?
My view:
create view int_objects_v as
select
io.*, soae."REQ_TYPE_NAME", soae."REQ_TABLE_NAME",
stc1."CODE_DESC" int_type_name, stc2."CODE_DESC" operation_type_name,
s."NAME" target_obj_name
from
int_objects io
left join
req_request_types_v soae on soae."ID" = io."SOURCE_OBJ_ID"
left join
std_type_codes_v stc1 on (stc1."CODE" = io."INT_TYPE"
and stc1."TYPE" = 'intr_type')
left join
std_type_codes_v stc2 on (stc2."CODE" = io."OPERATION_TYPE"
and stc2."TYPE" = 'opr_type')
left join
sysobjects s on (s."ID" = io."TARGET_OBJ_ID")
where
io."ACTIVE_FLAG" = '1';
PostgreSQL supports the SQL-standard information_schema.
So does MSSQL, but it also supports sysobjects which is older equivalent for information_schema.
If you find a way to rewrite your query using information_schema terms, then you're all set.
Also look here INFORMATION_SCHEMA vs sysobjects

Resources