problem in select into query - sql

Why m i getting error incorrect syntax near the keyword IN in the following query?
select * into persons_backup IN 'HRMS.mdb' from persons
Thank you

Assuming SQL Server (based on your previous question) you would need
select *
into persons_backup
from HRMS.mdb.persons
or
select *
into HRMS.mdb.persons_backup
from persons
dependant upon what you are trying to do exactly. See SELECT ... INTO syntax here

Assuming you want to add all rows from persons into another table persons_backup:
Insert into persons_backup select * from persons;
Depending on the RDBMS you use, you might have to put () around the select.

Related

SQL - Problem with subquery in the FROM clause

I am currently learning SQL and I tried testing something but it does not work.
The query that I tried is the following:
SELECT acc_id
FROM
(
SELECT *
FROM company
);
The inner query must return the whole table and the outter query must select from that table only a specific column. Simple as it seems this produces an error. The error message is:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6" (line 6 being the last line).
I can't figure out whats the issue.
You need to give an alias to your subquery:
SELECT acc_id
FROM
(
SELECT *
FROM company
) AS some_alias;
Although your query can be simplified into:
SELECT acc_id
FROM company;

Getting Count Of select Query

My Need is to get the count of query that gets Executed. I got the Query to get Count Like
Select Count(*) from ( Select Query) from myTable
but the problem is when user enters a special character like the comma, period etc it shows error like Syntax error at or near ')'. but from the user point of view, there is no ')'.
How do solve this? Is there any other way to get the count. My final output should Syntax error at or near ',' or '.' etc if they are present in the Query
You need sub-query and for that you can use CTE(common table expression)
Select Count(*) from ( ) from alias-- your sql
this way you can do post sql server and postgre
with t1 as
(
Select Query ---- your sql code
)
select Count(*) from t1 --- count from alis

How to get the latest value and the one right before that

I was reviewing this question/answer
Get latest date before date value in row
I thought I would give it a try, but every time I try the 'outer apply'
I get an error: Incorrect syntax near ')'.
In my query, my 'from' statement is not as simple as the example in the question & answer.
My 'from' has multiple joins and then at the every end of my joins I'm trying this:
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
)
Any additional help is much appreciated
I'm using SQLserver 2008 R2
Make sure you alias your outer apply. I don't know why SQL Server is always so unhelpful with this error.
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
) AS obligatory_alias
I would actually do something like the following
select ColumnsFromTableAYouCareAbout, max(b.datecolumn) as lastdate from mytable a
inner join mytable b on b.datecolumn < a.datecolumn
group by ColumnsFromTableAYouCareAbout

Why does FETCH FIRST N ROWS not work in combination with WITH statement?

I have the following SQL statement which does not run on my DB2 database:
WITH a AS (
SELECT * FROM sysibm.systables
)
SELECT a.* FROM a
FETCH FIRST 10 ROWS
Without the FETCH statement it works. The error message I get is:
Illegal use of keyword OPTIMIZE, token ERR_STMT
WNG_STMT GET SQL SAVEPOINT HOLD FREE
ASSOCIATE was expected.
Any suggestions?
You're missing the ONLY keyword at the end of the FETCH clause.
WITH a AS (
SELECT * FROM sysibm.systables
)
SELECT a.* FROM a
FETCH FIRST 10 ROWS ONLY;
Missing the Only Keyword at the end. Example here.
While the example you give is likely simplified, how about putting the fetch first clause in the first select portion?
I can never read the documentation clearly, but as the with-statement creates a common-table-expression, you might not be able to use the fetch-first-clause on selecting from it. According to this documentation, having the fetch-first-clause in the select of the with-statement is valid syntax.
i.e.
WITH a AS (
SELECT * FROM sysibm.systables
FETCH FIRST 10 ROWS ONLY
)
SELECT a.* FROM a;

Number of rows in Oracle SQL Select?

I need to know how many records were returned in a select in oracle. Currently, I do two queries:
SELECT COUNT(ITEM_ID) FROM MY_ITEMS;
SELECT * FROM MY_ITEMS;
I need to know the COUNT but I hate doing two queries. Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
If you want it to be in this exact order, you can fetch all records on the client and count their number (almost all client libraries provide a function for that).
You can also do:
SELECT i.*, COUNT(*) OVER ()
FROM my_items i
, which will return you the count along with each record.
If you're working in PL/SQL, you can use the SQL%ROWCOUNT pseudo-variable to get the number of rows affected by the last SQL statement. Might save you some effort.
This ought to do the trick.
WITH
base AS
(
SELECT *
FROM MY_ITEMS
)
SELECT (SELECT COUNT(*) FROM base) kount,
base.*
FROM base
I'm just unsure about the table aliases, I don't remember in Oracle if they require 'AS' or not. But this should work.
select mt.*, c.Cntr
from MyTable mt
, (select COUNT(*) as Cntr
from MyTable
) c