RODBC - multiple tables in one sqlQuery() call [duplicate] - sql

This question already has an answer here:
How to read multiple result sets returned from a SQL Server stored procedure in R
(1 answer)
Closed 3 years ago.
I'm using RODBC to pull data into R from an existing SQL database. I am trying to call multiple queries/tables at once.
For example:
test2=sqlQuery(channel,'select top 10 * from bug; select top 10 * from site')
This only returns the first table. I am interested in doing this because I want to call a more complex stored procedure that other collaborators are using and continuing development on...this procedure returns 3 tables (which are set as temporary tables in the stored procedure (#) to meet our other data needs/uses).
So, when I call the stored procedure, similar to the two query scenario above, it only returns the first table as the result.
Any suggested workarounds?

I don't think you'll be able to get that to work in RODBC; as far as I know, it expects a single resultset from the database.
However, you could modify your SQL to get both values in a single query:
SELECT
(SELECT COUNT(*)
FROM bug) AS bugcount,
(SELECT COUNT(*)
FROM site) AS sitecount;

Related

How to run sql query and loop through a column then pass that column data into another query from a second database

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.
Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.
SELECT *
FROM dbo.MYDB1
WHERE CreatedLoc = 123
The above example spits out data but I only care about the ID column
I than want to loop over the ID column and for each run them on another database.
SELECT *
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)
Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)
If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:
SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)

Filtering a SELECT statement based on row data [duplicate]

This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 7 years ago.
I'm trying to add a WHERE clause to a SELECT statement that checks to see if there is data in the table or if it was a 0-row table.
Basically, I'm trying to get this to work but obviously tables.NAME isn't a valid object.
SELECT NAME
FROM sys.tables
WHERE (SELECT Count(*)
FROM tables.NAME) <> 0
I don't want to have to create a temp table, declare a cursor and checking a value row by row, but I'm having a hard time thinking a bout how to do this otherwise.
Using a cursor is a suitable solution to this problem. You can't dynamically interpret a column from one table as a table name and simultaneously select from that table to see if it's empty like you're attempting to do. In fact you can't even select from a dynamic table without using dynamic SQL. See the documentation for FROM. You can either iterate over each table and check each, or you can query system tables that hold storage statistics to see if tables are empty.

Get row count of all tables in database: SQL Server [duplicate]

This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 8 years ago.
I'm trying to familiarize myself with a large database and search for relevant information among the many tables. I often find myself calling up a table, to see if there is relevant data inside, only to find that the table has no records.
How to quickly call up a list of all tables and the number of records contained therein? I'm using sql server 2008.
Thanks!
Related Question: How do I QUICKLY check many sql database tables and views to see if they are not empty or contain records
Right click on database -> Reports -> Standard Reports -> Disk usage by Top Tables
If you want to use a query, you can use this (note: it's using an undocumented stored procedure sp_msforeachtable):
create table #tempcount (tablename nvarchar(128), record_count bigint)
EXEC sp_msforeachtable 'insert #tempcount select ''?'', count(*) from ? with (nolock)'
select * from #tempcount
drop table #tempcount

SQL IN statement to extract for 100k records [duplicate]

This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I have a file SRQ which is having 10000 SRQ_ID which are unique.
I have one table(TABLE1) which is having 2 columns namely SRQ_ID,WORK_ID .
I needs to write a query which will search the table(TABLE1) for all the SRQ_ID's in the file SRQ and will display the output with corresponding WORK_ID.
I tried the below code. But IN clause is only applicable for 1000 records. How to run the same if I have 100k records?
select WO_ID
from TABLE1
where SRQ_ID in ('B6512DF0','5838FABC','EC5D804C','074DD65C')
Is there a reason you can't just do a join between the tables using SRQ_ID?
select wo_id from table1 join srq using srq_id
This will give you the work id for all rows that have a srq_id value in the srq table.
If the file s on the database server then you could access it as an external table, and join the two. Otherwise, I'd suggest bulk loading the codes into a global temporary table and performing the join against that.
In case you don't like to create a temporary table, you can use a nested table:
CREATE OR REPLACE TYPE VARCHAR_TABLE_TYPE AS TABLE OF VARCHAR2(20);
select WO_ID
from TABLE1
where SRQ_ID MEMBER OF VARCHAR_TABLE_TYPE('B6512DF0','5838FABC','EC5D804C','074DD65C');
But I don't know the limit for initializing a nested table. Oracle documentation says: "Because a nested table does not have a declared size, you can put as many elements in the constructor as necessary."

How to write an SQL query to get records based on the order they were entered in?(no date col) [duplicate]

This question already has answers here:
Is there a "Default Order By Column" in SQL Server?
(2 answers)
How to SELECT the last 10 rows of an SQL table which has no ID field?
(13 answers)
Closed 9 years ago.
I need to write a SQL query that returns records in the order in which they were entered in to a table.
I can't add a column to the table or change the table in any way. I can insert into and select from the table.
Say I execute three insert queries like
insert into x values(a,1);
insert into x values(d,4);
insert into x values(u,42);
when I select from the table x I need to get the records in this order.
a 1
d 4
u 42
The table has only two columns, both have nothing to do with date.
You can't do it without changing the table in some way.
When you select data from a table the order in which it is returned is non-deterministic on all the sql database engines I know and certainly in MSSQL server 2000+. To get the rows in a defined order you must include an ORDER BY clause and there is nothing you can specify to give the desired order.
Since you cannot change the schema, then this is game over.
Okay, (almost) nothing is impossible. You could periodically analyse the physical database file for changes and decode those into the information you require but, this would likely fail when multiple rows were inserted by one transaction.
I doubt you have the access or the inclination to do that.
You can't. The order returned is not deterministic without an order by clause, and you don't have any thing to order by