I want to get the first 95 data from DB2 in Visual Studio. I'm using table adapter and I have this query,
SELECT * FROM ASEINDTA.TRX_BWS WHERE (DKLDATE
= '2019-10-31') Fetch First 95 Rows Only
or this
SELECT * FROM ASEINDTA.TRX_BWS WHERE (DKLDATE
= '2019-10-31') ORDER BY Col[ 1 ]...Col[ n ]
Fetch First 95 Rows Only
But when I click Query Builder, this error appears.
But when I tried it in DBVisualizer, it works. How do I get that data? A help would be appreciate. Thanks
One approach would work on DB2 and most other databases would be to use ROW_NUMBER with a subquery:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM ASEINDTA.TRX_BWS
WHERE DKLDATE = '2019-10-31'
)
SELECT *
FROM cte
WHERE rn <= 95;
Or, the inlined version that does not use CTE:
SELECT *
FROM
(
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM ASEINDTA.TRX_BWS
WHERE DKLDATE = '2019-10-31'
) t
WHERE rn <= 95;
Remove the parentheses of the where clause:
SELECT * FROM ASEINDTA.TRX_BWS WHERE DKLDATE = '2019-10-31' Fetch First 95 Rows Only
It seems the issue is not on db2-server it-self, but from the tool you are using to execute the queries. You said, if you run the same in 'DBVisualizer' it works properly.
Looking at db2 documentation for this error: sqlcode -104 sqlstate 42601
it seems this error is returned by SYSPROC.ADMIN_CMD stored procedure.
This procedure is designed to run db2 administration commands in the target database, remotely. It was not designed to run queries... so, the parser for this proc is just a sub-set of db2 parser, just for specific admin commands.
So it's complaining about the FETCH token from your query.
It seems that the tool you are using 'I'm using table adapter' (no idea on what is it) , is calling this SYSPROC.ADMIN_CMD to execute the queries, but it should be using a regular CLI interface instead.
I don't know what exactly tool you are using. but try to see if it has some sort of settings, so you can change that behavior.
Here is the list of admin cmds that ADMIN_CMD proc can execute:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0012547.html .
as you can see, no SELECT statement there.
If I try to execute a simple SELECT , using this SP, I get the same error, from db2 CLP window, directly at the server.
db2 "call SYSPROC.ADMIN_CMD('SELECT * FROM DEPARTMENT')"
SQL0104N An unexpected token "SELECT" was found following
"BEGIN-OF-STATEMENT". Expected tokens may include: "ADD". SQLSTATE=42601
Regards
Samuel Pizarro
Ok
I had a closer look at the image error you posted...
the query is wrong. It's not exactly the same queries you posted in your question.
Have a closer look at the image error, and you will see that the Fetch and Rows words are double-quoted.
SELECT ... FROM WHERE (...) "Fetch" First 95 "Rows" Only
Remove the double-quotes from them.
If you are not writing them like that, so, it looks your tool is "changing" it, before submitting to the db2 engine. Again, not a db2 server issue.
Regards
Related
I have to remove the particular value from the sentence which is stored in SQL database. Sentence will look like this:
1 Payments:ihj - CHENNAI-HIRE:54005-TN69AZ54008,4021-TN69AZ54005
2 Payments:ihj - CHENNAI-HIRE:54004-TN69AZ54008,4021-TN69AZ54005,54005-TN69AZ54008
In above sentence 54004 is the number which I will pass as parameter to SQL. This is the number which I want to remove from this line but same number is present in this line as TN69AZ54005. This number should not be disturbed, and in another payment we have same amount in another place. Can anyone help on this?
I tried with this sql query
declare #text int=4019
select SUBSTRING(notes,CHARINDEX(cast(#text as varchar),notes),
len(notes)-CHARINDEX(',',notes)+1)
from Accounts.TransactionNotes
where TransactionID=1978
If I use this query it will affect including this line TN69AZ54005
I can see that you've included a C# tag into your question. Then probably the easiest way is just to select all necessary rows using your app, then iterate through them and change the strings to your needs (using eg. PHP preg_replace() equivalent) and update the SQL rows.
I believe that is the easiest way, not really SQL solution but still...
update <table> set notes = replace(notes, 'HIRE:'+ str(<inputparam>),'HIRE:') where transactionid=<transactionid>
update <table> set notes = replace(notes, ','+ str(<inputparam>),',') where transactionid=<transactionid>
You will need to find something to prefix your inputpram value, like in above example I am using "HIRE:" or a comma.
Another way could be to use REGEXP to find the whole word, then one one query would suffice. But I haven't tried it.
The problem here is not the query but the person who designed the
database.
I`m not sure is it this what you want but I will past my code. :)
-- for test create #temp
SELECT
A.DATA
INTO #Temp
FROM
(
SELECT 'Payments:ihj - CHENNAI-HIRE:54005-TN69AZ54008,4021-TN69AZ54005' AS DATA
UNION
SELECT 'Payments:ihj - CHENNAI-HIRE:54004-TN69AZ54008,4021-TN69AZ54005,54005-TN69AZ54008' AS DATA
) AS A
GO
-- this you want?
UPDATE #Temp
SET DATA = REPLACE(DATA,'54004','')
GO
-- select changed data
SELECT * FROM #Temp
I'm using the ADO.NET provider function "GetSchema" to fetch meta data out of a Sql Server database (and an Informix system as well) and want to know if there is anyway to paginate the results. I ask because one of the systems has over 3,000 tables (yes, three thousand) and twice that many views and let's not even talk about the stored procedures.
Needless to say, trying to bring down that list in one shot is too much for the VM I have running (a mere 4GB of memory). I'm already aware of the restrictions that can be applied, these are all tables in the "dbo" schema so there isn't much else that I'm aware of for limiting the result set before it gets to my client.
Instead of using GetSchema I suggest to use the more flexible INFORMATION_SCHEMA system views. These views already divide the information about the Tables, StoredProcedures and Views and you can write a specific query to retrieve your data in a paginated way.
For example to retrieve the first 100 rows of table names you could write a query like this
SELECT *
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY TABLE_NAME) AS RowNum, *
FROM INFORMATION_SCHEMA.TABLES
) AS TableWithRowNum
WHERE RowNum >= 0
AND RowNum < 100
ORDER BY RowNum
Following queries could be easily prepared changing the min and max values used by the query.
The same code could be applied for StoredProcedures (using INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE') or the views using INFORMATION_SCHEMA.VIEWS
Note, if you are using Sql Server 2012 and later the first query could be rewritten to use this syntax
SELECT *
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_NAME
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
And the C# code could also use parameters for the FIRST (0) and COUNT(100) values
I need to run a query on Mysql DB that retrieve the last 10 rows from a table which have no index column. I succeed to create the query-
SET #row_number:=0;
select activity from (
SELECT #row_number:= #row_number+ 1 AS rowNumber, activity
FROM activities a ) as myT
where myT.rowNumber > (select count(activity) from activities)- 10
But i need to run it through C#. And using MysqlCommand i can't create the parameter #row_number. Using command.Parameters.AddWithValue dosen't help
because the parameter need to be assigned when the query is executing.
(using the Parameters.AddWithValue produce the following error =
"..syntax to use near ':= 1 + 1...").
Thanks.
I think what you want is the LIMIT clause:
select activity from activities
LIMIT 10
I believe you'll have to create a stored procedure if you want to use a parameter for the LIMIT amount.
Maybe I'm misunderstanding your question but if you're only trying to grab the first 10 records why not use this:
SELECT TOP 10 [columnName] FROM [Table]
This returns the top 10 results in the database table.
or in C# you can set a variable
int rowNumber = 0;
then use the command.Parameter.add("#rowNumber", rowNumber);
that might work.
Hope this helps.
I have a linq query which selects several fields from my Customer table.
Applied to this method are multiple filters, using Func<IQueryable<T>, IQueryable<T>> with .Invoke.
The original query is essentially select * from customer.
The filter method is essentially select top 10
The output SQL is select top 10 from (select * from customer)
My customer table has over 1,000,000 rows which causes this query to take about 7 seconds to execute in SSMS. If I alter the output SQL to select top 10 from (select top 10 * from customer) by running it in SSMS then the query is instant (as you'd expect).
I am wondering if anyone knows what might cause LINQ to not combine these in a nice way, and if there is a best practice/workaround I can implement.
I should note that my actual code isn't select * it is selecting a few fields, but there is nothing more complex.
I am using SQL Server 2008 and MVC 3 with entity framework (not sure what version)
Edit: I should add, it's IQueryable all the way, nothing is evaluated until the end, and as a result the long execution is confined to that single line.
I don't know why it's not being optimised.
If the filter method really is equivalent to SELECT TOP 10 then you should be able to do it like this:
return query.Take(10);
which would resolve to select top 10 * from customer rather than the more convoluted thing you ended up with.
If this won't work then I'm afraid I'll need a little more detail.
EDIT: To clarify, if you do this in LINQ:
DataItems.Take(10).Take(10)
you would get this SQL:
SELECT TOP (10) [t1].[col1], [t1].[col2]
FROM (
SELECT TOP (10) [t0].[col1], [t0].[col2]
FROM [DataItem] AS [t0]
) AS [t1]
So if you can somehow use a Take(n) you will be okay.
I'm making a query that sorts and returns X rows based on row_number()
I'm using NHibernate with MSSQL and im trying to get paging working using CreateSQLQuery and i have this query:
select s.*
from(
select distinct release.[stop], survey.SurveyId, survey.Created, survey.CopyOfId, survey.DesignTemplateId, survey.UserId, survey.Template, [Row] = Row_Number() over (order by survey.[SurveyId])
from Survey as survey
inner join Release as release on release.SurveyId = survey.SurveyId
group by survey.SurveyId
, survey.Created
, survey.CopyOfId
, survey.DesignTemplateId
, survey.UserId
, survey.Template
, release.[stop]
) as s
where s.[Row] >= 0 and s.[Row] <= 20
order by s.[stop]
does anyone know how to get this working using HQL or ICriteria (even better) instead of plain SQL? The reason for this is that I want a query that is compatible with both SQLite and MS SQL Server 2005, so that i can use .SetMaxResult() og .SetFirstResult()
Thanks in advance!
Try to avoid using plain SQL in nHibernate.
On Criteria object, use SetFirstResult() and SetMaxResult() for your paging.
Pages of 10 records ? First page is criteria.SetFirstResult(0).SetMaxResult(10) and third page is criteria.SetFirstResult(20).SetMaxResult(10)
Always use the correct dialect. For exemple SQL Server 2008 has more paging features than SQL Server 2005.
Here's an excellent article to do what you want
http://www.tobinharris.com/past/2008/10/20/almost-iqueryable-with-nhibernate-hql/