ibatis Serach Queston when condition like Like '$propertyname$%' - ibatis

when i use ibatis,I have a sql like that
select * from user where name like '%$name$%'
if i set the username value is ???, it throw a exception,System.ArgumentOutOfRangeException.
i know i can do like that
mysql: select * from user where name like concat('%',#name #,'%')
oracle: select * from user where name like '%'||#name #||'%'
SQL Server:select * from user where name like '%'+#name #+'%
to avoid this question.
but i don't know why i use like '%$name$%' it throw exception.

Related

Getting query results in Workbench , Not in apex class/Script

Background: I need access account records in the apex class where name contains %
For example, the query should return below accounts
abcd10%
cdcd40%abcd
10%volume
When I execute the below query I am getting results in the workbench
select id, name from Account where Name LIKE '%\%%'
the same query If I execute in Anonymous window/apex execute getting no results.
List accList = [select id,name from Account where Name LIKE '%\%%'];
system.debug('accList :::'+accList.size());
If we observe the below image, it is appending extra / in the query and giving wrong results.
Can you please help me how to resolve this?
16:00:21:002 SOQL_EXECUTE_BEGIN [1]|Aggregations:0|SELECT id, name FROM Account WHERE Name LIKE '%\%%'
You could use a bind variable or dynamic SOQL.
String searchText = '%\\%%';
List<Account> accs = [SELECT Id, Name
FROM Account
WHERE Name LIKE :searchText];
System.debug(accs.size());
System.debug(accs);
// Or in dynamic query (looks bit crazier because ' has to be escaped too)
List<Account> accs2 = Database.query('SELECT Id, Name FROM Account WHERE Name LIKE \'%\\%%\'');
System.debug(accs2);

How to insert username in VS2008(report edition)

I'm creating a new report (*.rdl), and there I want to add username who runs the script (insert).
I've tried on VS2008 through "built-in-fields" function which is "User ID", but it didn't work:
CREATE TABLE #Some_Table
(
Plan_date date null,
Plan_customer int null,
creator_id nvarchar(55) null
)
INSERT INTO Some_Table
(
[Plan_date] ,
[Plan_customer],
[creator_id]
)
SELECT
#p_plan_monthly,
#p_plan_clients,
#creator_id ="user id" --from built-in-fields
Expected result is: Column creator_id is filling with value of username from active directory who made insert through my report.
To reiterate my comment, as it's is incredibly important:
"You need to use a different account to access your data #whitefang. The sa account should never be used for something as mundane as a report. In truth it should never be used unless you really need sysadmin privileges, or you're doing something like recovering the server. You should have a service account that can do the respective tasks it needs to. If you can suffer injection through those reports, you're service is like an open book to whomever has access."
Now, onto your problem. I would add a further internal parameter on your report. Change the value of the parameter to have the default value of =User!UserID; this will be the ID of the user running the report (perhaps something like StackOverflow\Larnu).
Then map that report parameter to your dataset parameter #creator_id and change your INSERT statement to:
INSERT INTO Some_Table ([Plan_date],
[Plan_customer],
[creator_id])
VALUES (#p_plan_monthly, #p_plan_clients, #creator_id);
Q: "and there I want to add username who runs the script (insert)"
You can use these functions.
-- database user name
SELECT USER_NAME()
-- login identification name
SELECT SUSER_NAME()

How parser properties in node XML file in XQUERY SQL server

I have this XML:
i want to get the value on Property name = "ParticipTypeName" i am using something like that:
;WITH XMLNAMESPACES(DEFAULT 'http://xml.common.asset.aoma.sonymusic.com/ProductMetadata.xsd')
SELECT
x.u.value('(/BusinessUnitProperties/Property[#name = "ParticipTypeName"])[1]', 'varchar(100)') as ParticipTypeName
from
#XML.nodes('/ProductMetadata/Tracks/Track/Participants/Participant') x(u)
it doesn't work.
How I should get the value in this property?
Try this:
SELECT x.u.value('(//*:Property[#*:name="ParticipTypeName"])[1]','nvarchar(max)')
The // will search for any element <Property>. The XQuery-filter will choose the one with the name you are looking for. The *: will allow you to ignore the namespace.

Access SQL Like * alternative in MS SQL server

I'm working on a project that has a search engine. AS we know in MS ACCESS we could use "*" in Queries under Criteria field to retrieve all records.
In SQL Server I need the same technique. I have tried different LIKE with WHERE Clauses. But I still didn't get the exact result I want.
In this project I have 3 textboxes (Category, Item, Location). If the user leaves any of them empty. I want to retrieve all the records.
I need something like this:
string t1,t2,t3;
if(!String.IsNullOrEmpty(txtCategory.Text))
t1=txtCategory.Text;
else
t1="*";
if(!String.IsNullOrEmpty(txtItem.Text))
t2=txtItem.Text;
else
t2="*"
if(!String.IsNullOrEmpty(txtLoc.Text))
t2=txtLoc.Text;
else
t3="*";
-
-
-
// in a function i have this :
SELECT * FROM Table_Items WHERE Category='"+t1+"' AND Item='"+t2+"' AND Location='"+t3+"'"
in ms sql server you can use the same technique but instead of * you should use %.
for examples:
%: means any
a%: all strings that start with a
%z: all strings that end with z
SO, your code should look like something as below:
// codes here
t3="%";
WHERE ColumnName LIKE t3
or
Where ColumnName LIKE '%'
I hope that will help you.
Change your * to the %
... Where Category Like "'+t1+"' and Item Like '"+t2+"' ...

Castle Active Record can not execute query because table name is equal to a keyword

I am trying to do the "getting started" from Castle Active record, now I got into this problem could not execute query exception
select count(*) as col_0_0_ from User user0_ where 1=1
//it's SQL Server 2008
question: Can I somehow make castle put the table name into [] like [User]?
In the [ActiveRecord] attribute, you can define the name of the table. Example:
[ActiveRecord("[User]")]
public class User : ActiveRecordBase<User>

Resources