This is my main query of my stored procedure (Sales):
select *
from Customers
where Customers Codes in ('11232133', '654664666',
'243423424', '123123231',
'967868686', '122343245', '636463636')
I want my stored procedure (Sales) connect it with Excel and refresh dynamically (VBA). Instead of customers codes that are contained in the IN clause, I want to show the customer name in the table in Excel and refresh dynamically to stored procedure.
What is the right process?
This is my table in Excel that I want to insert to my stored procedure dynamically:
Customers Codes
324516432
794536703
523682134
943321789
023456763
Thanks in advance!
Related
I have a DB2 stored procedure (my_proc) that returns two values. I want these two values to be displayed as two different columns.
Is the following correct way to use it?
select my_proc(parameter1,parameter2,parameter3) as column1,column2;
Please guide as I am learning DB2 and new to stored procedure.
Consider if it is possible or not for you to convert your stored procedure to a UDTF (user defined table function). This can be selected from and joined to and works very similar to a stored procedure.
https://www.itjungle.com/2016/06/14/fhg061416-story03/
I have a problem in Crystal reports 11. Because it has limitation to showing the information in dynamic reports and I have a huge list of customer, I want to write a query and ask the first letter of the customer name. If for example, it starts with A, just show the A customers.
this is my query:
select distinct bill_to_code
from TLORDER
where left(BILL_TO_CODE ,1 ) = ?
And CURRENT_STATUS <> 'CANCL'
AND LEFT(BILL_NUMBER , 1 ) NOT IN ('M','U','T','K')
but when I copy this to crystal reports and create a parameter , I get an error
Failed to retrieve data from the database
I should mention that I have another sql query in this report .
It is best to use Stored Procedure when trying to pass a parameter. How to create SP. Then used the SP for your report.
I am currently writing a VBA-based Excel add-in that's heavily based on a Jet database backend (I use the Office 2003 suite -- the problem would be the same with a more recent version of Office anyway).
During the initialization of my app, I create stored procedures that are defined in a text file. Those procedures are called by my app when needed.
Let me take a simple example to describe my issue: suppose that my app allows end-users to select the identifiers of orders for which they'd like details. Here's the table definition:
Table tblOrders: OrderID LONG, OrderDate DATE, (other fields)
The end-user may select one or more OrderIDs, displayed in a form - s/he just has to tick the checkbox of the relevant OrderIDs for which s/he'd like details (OrderDate, etc).
Because I don't know in advance how many OrderID s/he will select, I could dynamically create the SQL query in the VBA code by cascading WHERE clauses based on the choices made on the form:
SELECT * FROM tblOrders WHERE OrderID = 1 OR OrderID = 2 OR OrderID = 3
or, much simpler, by using the IN keyword:
SELECT * FROM tblOrders WHERE OrderID IN (1,2,3)
Now if I turn this simple query into a stored procedure so that I can dynamically pass list of OrderIDs I want to be displayed, how should I do? I already tried things like:
CREATE PROCEDURE spTest (#OrderList varchar) AS
SELECT * FROM tblOrders WHERE OrderID IN (#OrderList)
But this does not work (I was expecting that), because #OrderList is interpreted as a string (e.g. "1,2,3") and not as a list of long values. (I adapted from code found here: Passing a list/array to SQL Server stored procedure)
I'd like to avoid dealing with this issue via pure VBA code (i.e. dynamically assigning list of values to a query that is hardcoded in my application) as much as possible. I'd understand if ever this is not possible.
Any clue?
You can create the query-statement string dynamically. In SQL Server you can have a function whose return value is a TABLE, and invoke that function inline as if it were a table. Or in JET you could also create a kludge -- a temporary table (or persistent table that serves the function of a temporary table) that contains the values in your in-list, one per row, and join on that table. The query would thus be a two-step process: 1) populate temp table with INLIST values, then 2) execute the query joining on the temp table.
MYTEMPTABLE
autoincrementing id
QueryID [some value to identify the current query, perhaps a GUID]
myvalue one of the values in your in-list, string
select * from foo
inner join MYTEMPTABLE on foo.column = MYTEMPTABLE.myvalue and MYTEMPTABLE.QueryId = ?
[cannot recall if JET allows ANDs in INNER JOIN as SQL Server does --
if not, adjust syntax accordingly]
instead of
select * from foo where foo.column IN (... )
In this way you could have the same table handle multiple queries concurrently, because each query would have a unique identifier. You could delete the in-list rows after you're finished with them:
DELETE FROM MYTEMPTABLE where QueryID = ?
P.S. There would be several ways of handling data type issues for the join. You could cast the string value in MYTEMPTABLE as required, or you could have multiple columns in MYTEMPTABLE of varying datatypes, inserting into and joining on the correct column:
MYTEMPTABLE
id
queryid
mytextvalue
myintvalue
mymoneyvalue
etc
i have a parent report and it contains a two sub report.
* subreport: item
which get all fields from store procedure named spGetReportItem. like
ItemName ItemQuantity TotalItemCost
ab 4 45
dd 6 98
*subreport: Labour
which get all fields from store procedure named spGetReportLabour. like
labourName labourQuantity TotalLabourCost
ab 44 455
dd 63 986
i want to find the total of totalitemcost and total of totallabourcost and then want grandtotal of totalitemcost and totallabourcost.
i have seen many examples on internet in which shared variable is used in the formula bt the problem is that they have used the table but i m fetching data from stored procedure. so how can i access the stored procedure fields for calculation.
like i have seen that many have used:
shared numbervar total:=sum({tablename.ColumnName});
but i have used stored procedure instead of table so how could i find total of field that resultset returns from stored procedure..
plz give me answer as soon as possible..
i need it urgently.
thanks..
The stored procedure in the end should be having a select statement in the end that returns the data. use the tablename in the select statement and that should work out.
It does not matter if its stored procedure or table. To crystal report its just source of data.http://msdn.microsoft.com/en-us/magazine/cc301570.aspx
And you should be able to find out how its represented from the Field Explorer. Just expand the Database fields and drag it on it a section and you can get the representation.
I currently have a stored procedure that returns a list of account numbers and associated details. The result set may contain multiple entries for the same account number. I also want to get some aggregate information such as how many distinct accounts are contained within a particular result set. Is there some way to retrieve such a view from my stored procedure results such as
SELECT AccountNumber, Count(*)
FROM mystoredproc_sp
GROUP BY AccountNumber
It's fine if it needs to be contained within another stored procedure, but I'd like to be able to at least benefit from the logic that already exists in the first SP without duplicating the bulk of its code.
DECLARE #tt TABLE (acc INTEGER)
INSERT INTO #tt EXECUTE mystoredproc_sp
SELECT acc, COUNT(*) FROM #tt GROUP BY acc
You would have to move your query into a table-valued function and call it from both stored procedures - the old one and the new one. That way you have the query in only one place. It is not possible to select from a stored procedure result set.