In ClearQuest, How do I generate a query in SQL Editor that allows me to prompt the user for a value? - sql

I generate a ClearQuest query using the Query Wizard. In the Query Editor, I am able to select a filter for a given field. However, I want to refine the query using the SQL Editor, but then I loose the ability to have a dynamic filter. How can I resolve this.

Pretty sure the answer is "you can't" since the SQL generated includes the value entered for any dynamic filters set up when you built the query, and as you say, the act of editing the SQL prevents using the Query Editor to make further changes.
Your best bet is to figure out a way to use the Query Editor to do what you need the SQL Editor for. Start another question.

Related

Dealing with filtered Pass Through Query in MS Access

I have a relatively complex SQL query (complex to run in Access) and want to run it in MS Access. It works with the pass-through query well but going forward I will face an issue that is related to a filter I apply in the query. I select the current report date within the where function. Below is a part of my query I try to handle ;
select LS.PID_FACILITY, LS.ASOF_DTE, LS.DATA_CYCLE_FLG, LS.CUST_ACC, LS.CUST_SMUN, LS.CUST_NME, LS.CUST_CTY,
WHERE LS.ASOF_DTE='19-SEP-22'
I do not want to change asof_dte filter manually everyday. If this was a normal access query I could join another table that includes only the current report date. But I cannot do it in a pass-through query. What is the alternative way to do it? I read something about creating variables or strings, but I could not relate them to my problem, since I am a beginner at creating such solutions.
Thank you all.
Well, two VERY intresting things here.
First, YES a great idea to include the date in the PT query. But, you don't want to change that date each time.
Soluton:
Add a paramter to the query, and then from Access code add that paramter. It is VERY easy to do this (one line of code!!! - don't adopt the zillion examples out there that has a boatload of ADO code - NOT required!.
However, BEFORE we start dealing with above?
A MUCH better and simple, less work way to approach this?
in place of stored procedure?
if possbile, create a view. and use that for the report.
Why?
Because you then get TWO VERY valuable bonus.
First, you can freely use the reports "where" clause, and it respects the where clause and STILL runs server side!!!
In other words, create a view for that existing query, but WITHOUT the date set in that view.
You then link to the view from access client side.
Now, to open (filter) the report, you can do this:
docmd.OpenReport "MyReport",acViewPreview,,"LS.ASOF_DTE='19-SEP-22'"
Now, of couse the above "where" clause can be a varible (string).
NOTE SUPER but SUPER careful here:
If you base the reprot on a pass-though query (that then uses the stored procedure), then the filter occures CLIENT SIDE!!!! (all rows will be returned and THEN filtered if you report is based on that stored procedure.
But, if you use a view?
The the filter makes it to the server side!!!!
While both the pass-through query or the "view" can be filtered with the above "open report" and the where clause we have above?
The view will still filter server side - the pass-though query will NOT!!!
Now, the 3rd way, is of course to build the stored procedure to accept a date parmater.
You then could do this:
with Currentdb.QueryDefs("MyPassThoughQueryGoesHere")
.SQL = "EXEC MyStoreProc " + "19-SEP-22"
END WITH
docmd.OpenReport "MyReport",acViewPreview
So, you CAN add and have a PT query and add a paramter as per above.
However, unless that stored procedure has some speical code, you are MUCH better off to create a view server side, base the reprot on that view, and simple pass + use the traditional "where" clause of the open report command. Even if that view has no filter, returns all rows in the table?
With the "where" clause of the open report command, ONLY those rows meeting that critera will be pulled down the network pipe.
So, say a invoice table with 1 million rows.
Create a view, link the view in access.
base report on that view.
Now, do this:
docmd.OpenReport "rptInvoice",,,"InvoiceNum = 134343"
The above will ONLY PULL down 1 row from the server. Even if the view has no filter and would return 1 million rows.
So, using a view is less work then creating the stored procedure.
But, you can modify the stored procedure to accept a paramter, and then as noted use the above example to modify the PT query you have, and THEN open the report.
I think overall, it is less work to use view. Furthermore, if you have a slow running report now?
Replace the query (move it) to sql server side. Get it working. Now link to that view (give it same name as what the client side query was in Access).
Now, EVEN if you had some fancy filter code in VBA, and used openReport with the "where" clause? It will now work, only pull the records down the network pipe, you get stored procedure performance without the hassles. and the date format and "where" clause for open report is access/VBA style - not sql server style SQL.
So, high recommend you try and dump the stored procedure and use a view (and EVEN better is any where clause works - not just one based on pre-defined parameters for the stored procedure - so you not limited to parameters)
. However, no big deal - the above "EXEC dbo.MyStoreProce " & strDate example would also work fine if you have a date parameter you wish to supply to the pass-though query.

Cannot view the SQL portion of a query in ACCESS?

I am currently working on a project of replacing our old access database queries, but on one of them I am not able to view the actual SQL View.
Does anyone know a way to force the view or to export it somehow?
Error causing problem:
The SQL statement could not be executed because it contains ambiguous outer joins.
Note that I can view the Design View without issue but when I right click on the tab and select SQL View is when I get the error.
I did attempt what #LeeMac mentioned below but same error occurs:
EDIT:
This question is not like Ambiguous Outer Joins?
The OP on that question can actually see and edit their SQL.
My issues is that I cannot see or edit the SQL as the SQL View wont open.
Try executing the following VBA code from the Immediate Window (accessible using Ctrl+G) in the VBA IDE (open the IDE using Alt+F11):
?CurrentDb.QueryDefs("YourQuery").SQL
Replace YourQuery with the name of your query.
This should print the SQL code which comprises your query - you can then analyse the SQL to determine the cause of the error.
It's odd this error would arise when merely viewing the SQL content of the query definition.
It makes me think that the query is perhaps referencing a crosstab subquery which is actually the cause of the error, but which needs to be evaluated in order for MS Access to determine the columns available when viewing the design of the query in question.
Try this:
hit ctrl-g, and from immediate window type in this:
saveastext acQuery,"Name of query","c:\test\mysql.txt"
Access ordinarily doesn't allow you to save invalid queries, so it's strange you somehow got into this situation in the first place.
If you can copy the query, you can easily get to the SQL by changing the query to a passthrough query, either through the GUI or through VBA:
Dim q As DAO.QueryDef
Set q = CurrentDb.QueryDefs!Query1
q.Connect = "ODBC;"
Debug.Print q.SQL
Passthrough queries are not validated, so you can freely read and write anything you want as SQL in it.
Note that this is irreversible when done through VBA. You can only change it back to a normal query once you made the SQL valid again. If you do it through the GUI, you can just not save it, though.
I had this problem and the issue was that i had a subquery that calculated fields but did not actually have a table in it. for example it would calculate first and last day of last month which is 2 calculated fields, then it was the first query in a series of queries that were built off it and the last one wouldnt resolve sql as original poster indicated also gave the ambiguous join message as well as query needs input table (which was that first subquery). i put a table with 1 record in it but didnt use the record and it worked.... so it just a needs a table in it.

find column name in sql query

I would like to use the GUI to find q column name in my sql query which has more than 300 columns
For example: I want to use GUI provided by SQL to find all the column names starting with VENDOR and hi-light them in order to make my operation easier..
I just want to use GUI
P.S: No query suggestions please, I am already aware of them (using like and all)
In MSQL Studio, use the Query Designer (Design Query In Editor...), paste your SQL, then use the name column in criteria pane to copy and paste in excel

Execute VBA using SQL

Ok, this may be a long shot but in MS Access, is it possible to execute VBA using SQL? I'm looking to write a query that runs either a single VBA statement or a Sub. Basically, what I'm trying to do is modify the hidden attribute of a table using SQL.
MS makes enough weird decisions with Office implementations that I'm mildly hopeful this is one of those cases.
You can create a VBA function and use the function's return value as a field expression in a query. However, you must run the query from within an Access application session. Otherwise the db engine will not be able to use your custom function.
SELECT YourFunction() AS result;
It's been a few years (thankfully) since I used Access, but to the best of my knowledge, that's simply not possible. You can use VBA to execute SQL, but not vice versa.
You can use user-defined functions within an SQL query ( http://www.sqlexamples.info/SQL/inlineudf.htm ). I doubt Excel checks what you do inside this function, so you probably can do whatever you like.

How to edit SQL records using Select statement

I got like 2000 rows of data, when I do select statement I can narrow my query but I cant change any value, as I have to change 50 columns, I dont want to use Update Command as then because values I have to update for each row is unique.
Anyone knows any easier way of selecting data using select command and then edit it.
EDIT
I just went to SQL Management studio, clicked on DB I am working on then find the table, I right clicked on it and then it says "Select top 1000 rows" so Now I can see the query and the data, I added "Where" into query and got what I wanted, but I cant modify the table rows below :S
*Edit 2 *
Or I can develop a Utility that will take Table Name , Column Name and its New value and simply updates it :)
You modify data using an UPDATE statement (with a WHERE clause) in a query window. While Management Studio has a feature called "Edit Top n Rows" that doesn't mean it's a good idea to use it - there are several behavioral bugs that are still unresolved even in the SQL Server 2012 version, and it can also place unnecessary and prohibitive locks on the underlying table.
I know it's not the answer you want to hear, but please become comfortable with proper DML commands. The documentation for UPDATE is found here:
http://msdn.microsoft.com/en-us/library/ms177523(v=sql.100).aspx
The long and short of it, IMHO: If you can identify the rows you want to update by using a SELECT with a WHERE clause, you can also write an UPDATE query using the same WHERE clause.
In SSMS - in the same place you saw "Select top 1000 rows", there should be another option "Open table" or "Edit top 200 Rows" (by default).
If you did that on a table, then pressed the following button:
You should then by able to paste in your select statement you've already written instead of what is there, press the execute button (Red exclamation mark button). The grid that appears is editable.
In general I'd be inclined to agree with #AaronBertrand though, it's better to learn the syntax to Update statements yourself.
If you have Microsoft Access, you can create a linked table to your sql server database and edit it by opening the table and modifying the relevant rows. You can create a query in Microsoft Access as well limiting the rows you want to return, and then you can inline edit the columns you'd like to change in the result set.
See for example here how to create a linked table to SQL Server.