asp sqldatasource text - asp.net

I need to have a sqldatasource with the following sql in it.
if #filter = 'departments'
begin
SELECT ISNULL(DocTitle,'') as Name, DocNumber as id, DocUrl+DocName AS link,LastModBy,LastModDate, IsLink
FROM cmc.CMC_Docs d
INNER JOIN CMC.CMC_Doc_Locations as l on l.FamilyID = d.FamilyID
INNER JOIN CMC.CMC_DocFamilies df on df.FamilyID = d.FamilyId
WHERE IsEnabled=1
AND ISNULL(DocName,'') <> ''
AND d.FamilyID IN #dep
ORDER by DocTitle
end
where #dep is something like (2,3)
However when I try to test the query I get an error saying incorrect syntax near #dep.
Any ideas how I need to write this inside of the datasource in order for it to work?
Thanks,

do you need to put this in ()?
ex: select * from product where productid in (1,2,3) works
ex: select * from product where productid in 1,2,3 - does not work

Related

Teradata dynamic SQL getting code from another table

I am trying to recreate a query that uses a subquery in the left join to get the value of a KPI. I am trying to store that subquery in another table as a string and use dynamic sql to retrieve that string code and insert it into the left join, but I am getting an error:
[5526] SPL2010:E(L27), Variable of CHARACTER type expected in PREPARE or EXECUTE IMMEDIATE statement.
Here is the procedure I am trying to create. In the variable metric_code, the column SQL is a string that contains the sql code.
Create PROCEDURE dyn_sql_test()
BEGIN
DECLARE metric_code VARCHAR(5000);
Set metric_code = (Select SQL_CODE from mytable_with_Sql_code where ID=1234);
BEGIN
set get_value='
SELECT
e.*
,a.value
FROM Metric_table AS e
LEFT JOIN ('||metric_code||') a
on e.id=a.id
WHERE e.ID = 1234;';
EXECUTE IMMEDIATE get_value;
END;
END;
In case it helps, the string I am retrieving for SQL_CODE, which used to go in the Left Join as a subquery looks something like this:
sel id,type,values
from my_values_table
where type='kpi'

Stored Procedure value to asp.net page under text box

I have a store procedure which show single row value like
(x.CLTotalAc, x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL).
I want these value to my asp.net C# page under text box (txtCLTotalAmt,
txtCL, txtGB_CLTotalAmt, txtGB_CL)
Store Procedure
ALTER proc [dbo].[Cr_CL_Report]
#ProductName text,
#BranchName text
as
begin
Select x.CLTotalAc,x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL
from
(
SELECT COUNT(dbo.DDBranchName.BCode) AS BrCode,
dbo.TblTotalCL.CLProductName AS ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS CLAc, SUM(dbo.TblTotalCL.CLAmt) AS CLAmt,
SUM(dbo.TblTotalCL.CLAmt)/SUM(dbo.TblTotalCL.CLTotalAmt) * 100 AS CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
AND (dbo.DDBranchName.BCode LIKE #BranchName)
GROUP BY dbo.TblTotalCL.CLProductName
) x
inner join
(
SELECT COUNT(dbo.DDBranchName.BCode) AS GB_BrCode,
dbo.TblTotalCL.CLProductName AS GB_ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS GB_CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS GB_CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS GB_CLAc,
SUM(dbo.TblTotalCL.CLAmt) AS GB_CLAmt,
(SUM(dbo.TblTotalCL.CLAmt) / SUM(dbo.TblTotalCL.CLTotalAmt))*100 AS GB_CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
GROUP BY dbo.TblTotalCL.CLProductName
) y on
x.ProductName = y.GB_ProductName
end
Blockquote
how can I solve this?
Getting return value from stored procedure in C#
In the link you can see how to get the values form a stored procedure. Then assign the value to the text box
Start by reading up on Ado.net. Use for instance the example here,under adonet data provider examples/sqlclient. When you have this working you have retrieved the data from the database. (imho avoid table adapters, entity framework and linqtosql if you want to grok it. instead go with manually writing for SqlCommand. or dapper to ease some of the burden.)
Now you have to get it to the web page. First decide on Webforms or Aspnetmvc. Then googlewithbing what the syntax is like.
Wash.
Rinse.
Repeat.
HTH

I am getting an error when I execute a delete query

I am executing the following query
DELETE FROM List,Tree WHERE List.CatID = Tree.CatID AND List.ID = '1' AND Tree.Cat = '332'
but I run into following error
near ",": syntax error
Correct syntax for delete statement is this
DELETE FROM table_name WHERE somecolumn=somevalue
So you cannot use 2 tables in a single delete query by separating them with a comma..
You need to do something like
DELETE something FROM table_name INNER JOIN...
You can delete only from one table at a time, and you have to rewrite the join as a subquery:
DELETE FROM List
WHERE ID = '1'
AND CatID IN (SELECT CatID
FROM Tree
WHERE Cat = '332')

PL/SQL - comma separated list within IN CLAUSE

I am having trouble getting a block of pl/sql code to work. In the top of my procedure I get some data from my oracle apex application on what checkboxes are checked. Because the report that contains the checkboxes is generated dynamically I have to loop through the
APEX_APPLICATION.G_F01
list and generate a comma separated string which looks like this
v_list VARCHAR2(255) := (1,3,5,9,10);
I want to then query on that list later and place the v_list on an IN clause like so
SELECT * FROM users
WHERE user_id IN (v_list);
This of course throws an error. My question is what can I convert the v_list to in order to be able to insert it into a IN clause in a query within a pl/sql procedure?
If users is small and user_id doesn't contain commas, you could use:
SELECT * FROM users WHERE ',' || v_list || ',' LIKE '%,'||user_id||',%'
This query is not optimal though because it can't use indexes on user_id.
I advise you to use a pipelined function that returns a table of NUMBER that you can query directly. For example:
CREATE TYPE tab_number IS TABLE OF NUMBER;
/
CREATE OR REPLACE FUNCTION string_to_table_num(p VARCHAR2)
RETURN tab_number
PIPELINED IS
BEGIN
FOR cc IN (SELECT rtrim(regexp_substr(str, '[^,]*,', 1, level), ',') res
FROM (SELECT p || ',' str FROM dual)
CONNECT BY level <= length(str)
- length(replace(str, ',', ''))) LOOP
PIPE ROW(cc.res);
END LOOP;
END;
/
You would then be able to build queries such as:
SELECT *
FROM users
WHERE user_id IN (SELECT *
FROM TABLE(string_to_table_num('1,2,3,4,5'));
You can use XMLTABLE as follows
SELECT * FROM users
WHERE user_id IN (SELECT to_number(column_value) FROM XMLTABLE(v_list));
I have tried to find a solution for that too but never succeeded. You can build the query as a string and then run EXECUTE IMMEDIATE, see http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/dynamic.htm#i14500.
That said, it just occurred to me that the argument of an IN clause can be a sub-select:
SELECT * FROM users
WHERE user_id IN (SELECT something FROM somewhere)
so, is it possible to expose the checkbox values as a stored function? Then you might be able to do something like
SELECT * FROM users
WHERE user_id IN (SELECT my_package.checkbox_func FROM dual)
Personally, i like this approach:
with t as (select 'a,b,c,d,e' str from dual)
--
select val
from t, xmltable('/root/e/text()'
passing xmltype('<root><e>' || replace(t.str,',','</e><e>')|| '</e></root>')
columns val varchar2(10) path '/'
)
Which can be found among other examples in Thread: Split Comma Delimited String Oracle
If you feel like swamping in even more options, visit the OTN plsql forums.

NHibernate Collection Left Outer Join Where Clause Issue

It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.

Resources