select not working after putting where condition in nicknames? - sql

I have a remote mainframe db2 database for which I have created nicknames in my db2 server .
Problem is as below -
When I run query
SELECT * FROM LNICKNAME.TABLE - It runs and I can get all columns.
but if I run below query it never gives me any output and keeps running .
SELECT * FROM LNICKNAME.TABLE a where a.columnB = 'ADH00040';
So technically it does not work if I add any where condition .

It doesn't seem like there is an error with your SELECT statement. So I am assuming that one of two things are happening:
Senario 1:
The file is really big and there isn't an index on columnB. If this is the case it would take long as the DB would have to read through each record and check if columnB = 'ADH00040'. To see how many records are in the table just run a count on the table
SELECT COUNT(*) FROM LNICKNAME.BMS_TABLE
Senario 2:
Something or someone is disconnecting your connection before your query is complete. I know you can limit the amount of CPU time a iSeries job is allowed before it gets ended forceably (CHGJOB CPUTIME(60000)). Is there no form of a job log that you could share with us?

Are you sure your value is into your table?
try a like :
SELECT * FROM LNICKNAME.TABLE a where a.columnB like '%ADH00040%';

Related

Query just runs, doesn't execute

my query just runs and doesnt execute, what is wrong. work on oracle sql developer, company server
CREATE TABLE voice2020 AS
SELECT
to_char(SDATE , 'YYYYMM') as month,
MSISDN,
SUM(CH_MONEY_SUBS_DED)/100 AS AIRTIME_VOICE,
SUM(CALLDURATION/60) AS MIN_USAGE,
sum(DUR_ONNET_OOB/60) as DUR_ONNET_OOB,
sum(DUR_ONNET_IB/60) as DUR_ONNET_IB,
sum(DUR_ONNET_FREE/60) as DUR_ONNET_FREE,
sum(DUR_OFFNET_OOB/60) as DUR_OFFNET_OOB,
sum(DUR_OFFNET_IB/60) as DUR_OFFNET_IB,
sum(DUR_OFFNET_FREE/60) as DUR_OFFNET_FREE,
SUM(case when sdate < to_date('20190301','YYYYMMDD')
then CH_MONEY_PAID_DED-nvl(CH_MONEY_SUBS_DED,0)-REV_VOICE_INT-REV_VOICE_ROAM_OUTGOING-REV_VOICE_ROAM_Incoming
else (CH_MONEY_OOB-REV_VOICE_INT-REV_VOICE_ROAM_OUTGOING-REV_VOICE_ROAM_Incoming) end)/100 AS VOICE_OOB_SPEND
FROM CCN.CCN_VOICE_MSISDN_MM#xdr1
where MSISDN IN ( SELECT MSISDN FROM saayma_a.BASE30112020) --change date
GROUP BY
MSISDN,
to_char(SDATE , 'YYYYMM')
;
This is a performance issue. Clearly the query driving your CREATE TABLE statement is taking too long to return a result set.
You are querying from a table in a remote database (CCN.CCN_VOICE_MSISDN_MM#xdr1) and then filtering against a local table (saayma_a.BASE30112020) . This means you are going to copy all of that remote table across the network, then discard the records which don't match the WHERE clause.
You know your data (or at least you should know it): does that sound efficient? If you're actually discarding most of the records you should try to filter CCN_VOICE_MSIDN_MM in the remote database.
If you need more advice you need to provide more information. Please read this post about asking Oracle tuning questions on this site, then edit your question to include some details.
You are executing CTAS (CREATE TABLE AS SELECT) and the purpose of this query is to create the table with data which is generated via this query.
If you want to just execute the query and see the data then remove first line of your query.
-- CREATE TABLE voice2020 AS
SELECT
.....
Also, the data of your actual query must be present in the voice2020 table if you have already executed it once.
Select * from voice2020;
Looks like you are trying to copying the data from one table to another table, Can you once create the table if it's not created and then try this statement.
insert into target_table select * from source_table;

sql server shortcut for select statement writing

Many times a day I have to write similar queries to get single record:
select t.*
from some_table t
where t.Id = 123456
maybe there is some shortcuts for retrieving single record? Like entering id, table and SQL server generates rest code automatically
In Sql Server Go to
Tools-> Options-> Environments->Keyboard
You will get shortcuts, there you can define your own as well as get the standards.
you can set a short cut for a fully executable query like
select * from table where id =20
but not like below
select * from

Sql query to select records starting with +

Basically I have a user table where mobile is not stored. I want to run a query to select all records starting with +91.
My current query is
Query - select count(*) from temp_table where cell_phone_no like "+91%"; Results - 0
While I know for a fact that there are certain rows with mobile numbers starting with +91.
Important: you must to know us your DBMS, because the sintax can be different, but I try to answer to your question
If you have really these rows (the row as + 91 is not good), your query can be re-write in this way:
select count(*) from temp_table where cell_phone_no like '+91%'
So, I've changed the " with a single quote '
You should check for other hidden values. I might suggest starting with this:
select cell_phone
from temp_table
where cell_phone_no like '%+%91%'
This should get any number that has a + and a 91 in it somewhere. If you get rows from this query, you need to investigate why yours doesn't work.

Delete Query inside Where clause

Is there any possibility to write delete query inside Where clause.
Example:
Select ID,Name From MyTable Where ID IN(Delete From MyTable)
It may be crazy, but let me explain my situation. In our reporting tool, we are supporting to enter SQL where query.
We will use our own Select and From Clause query and combine the user's where query input.
Example:
Select ID,Name From MyTable Where ("Query typed by user")
Here, user can type any kind of where query filter..
If he types like ID=100 our final query becomes like this
Select ID,Name From MyTable Where (ID=100)
One of our customer asked us what will happen if anyone type the delete query as where query filter. he feels this may be the security hole..so we have tried that kind of possibility in our dev environment. But the sql returns error for the following query.
Select ID,Name From MyTable Where ID IN(Delete From MyTable)
So finally, my question is, is there any other possibility to write Delete Query inside Where clause or Select clause.. If it possible, how can I restrict it?
Yes. They can run a delete. They can type:
1 = 1; DELETE FROM MY_TABLE;
Or even worse in some ways, (since you should have backups):
1 = 0 UNION SELECT SOCIAL_SECURITY_NUMBER, CREDIT_CARD_NUMBER, OTHER_SENSITIVE_DATA FROM MY_SENSITIVE_TABLE;
Now, in your case its hard to validate. Normally if you are just passing a value to filter on you can use parameterised sql to save yourself. You however also need to let the user select a column. In cases like these, usually we use a drop down to allow the user to select a predefined list of columns and then validate the column name server side. We give the user a text box to enter the value to match and then parameterise that.
It's not quite possible. But he can do something like this :
Select ID,Name From MyTable Where (ID=100); (DELETE FROM MyTable Where 1 = 1)
by using ID=100); (DELETE FROM MyTable Where 1 = 1 instead of ID=100
I believe what your customer is talking about is SQL injection, as long as you have taken appropriate methods to block other queries from running after your select statement is done, then you should have no problem in letting them type whatever it is that you want.
From my experience there is no way to delete anything when you are doing a select statement.
Just make sure you have query terminator characters so they don't write something like the following.
select column1,column2, from myTable where ID in (1,2); delete from my table
this would be a valid worry from your customer if you aren't taking proper steps to prevent sql injection from happening.
You could have your SQL reporting tool just not have update, or delete permission and just have it have Read permission. However, it is up to you guys have you handle your sql injection security.

Why does "select count(*)" from nothing return 1

With SQL Server 2012 :
use master
select *
yields
Must specify table to select from
which is exactly what I would expect.
But the funny thing is that
use master
select count(*)
returns 1.
Can someone explain to me what is counted here?
Edit : And possibly include sources...
SQL Server is (behind the curtain) effectively applying a from to a dummy table, which has only one row. Thus you will get 1 for your count.
select 'test'
will do the same thing, as an example, return 'test' one time.
It's like the DUAL table in Oracle, SYSDUMMY1 in DB2, etc.
As requested, here's a couple of links to MS Connect on this topic:
Clicky
More Clicky