Regex for searching Database-Statements in VisualStudio - visual-studio

I am currently working on a rather large project in Visual Studio 2008 with a lot of Database-Statements. The Statements are held in strings like this:
string stmt = "SELECT ID, OTHER " +
"FROM TABLE " +
"WHERE CONDITION";
I was wondering how to find all Statements via regex. So I am not so good at regex, but maybe someone got any idea? I don't know if it's impossible because of the multilining? Does it work with the search inside Visual Studio?
EDIT to answer of Clement: Well SQL-Statements are not only SELECT-Statements, in my case there are also a lot of UPDATE- and INSERT-Statements. But what if there other eg. CREATE-Statements?

CTRL + F, Choose "Active Project", and specify "SELECT" as token, no ?

Run a regular expression that uses the '|' operator which acts as an or. An example would be this:
Text To Search
"INSERT INTO table; SELECT * FROM table; UPDATE table; DELETE FROM table"
RegEx expression
INSERT|SELECT|UPDATE|DELETE
This returns these values along with their index
INSERT
SELECT
UPDATE
DELETE

Related

Oracle SQL developer - What is the easiest way to surround a list of texts with quotes and commas?

Here's an example. I get the list like this below
99X2H19JBF11534
16B4H5FHCA12592
ABE4H5FHAA08646
SE0753207527
PAK810869145
PAK810714143
PAK810547887
PAK810340854
PAK820090918
I then use concatenate in excel (and surround the list with quotes and commas) to run queries like below in Oracle SQL Developer.
select * from admin.repair
where repair_nbr in (
'99X2H19JBF11534',
'16B4H5FHCA12592',
'ABE4H5FHAA08646',
'SE0753207527',
'PAK810869145',
'PAK810714143',
'PAK810547887',
'PAK810340854',
'PAK820090918');
I was wondering if other people have an easier way to surround the list with quotes and commas in Oracle SQL developer.
If you got your list from a query, just drag and drop.
We'll not only quote the strings, we'll separate them with commas for you.
If you just have the text, SQL Developer also support block editing. Enable that on the Edit menu, then simply use down arrow to go through each line/curpos and select then paste the quotes.
Or if you're a fan of regex, the Search and Replace (ctrl+R) panel has a RegEx mode you can toggle on.
Disclaimer: I'm a product manager at Oracle for SQL Developer.
Assuming you got your list using query
select val from your_table order by whatever
change your query to
select 'select * from admin.repair where repair_nbr in ('
|| listagg('''' || val || '''', ',
' order by whatever)
|| ');'
from your_table
The result of auxiliar query is the query you require.
Note: You might get ORA-01489 if the result of concatenation is too long but it is different - yet still solvable - story (look for xmlagg function then).

Oracle SQL Developer - way to add comma to the end of each line for all items in the IN clause

I can't be the only person who has run into this, but I have not been able to find anything regarding this question. I'm hoping there is a simple solution that I just haven't come across. What I would like to have is a way in SQL Developer to simply append a comma to a selection of lines, so a statement like this:
select * from orders where order_number in (
10000
10001
10002
10003
)
Would turn into this:
select * from orders where order_number in (
10000,
10001,
10002,
10003
)
The comma can be at the beginning or end of the line as long as it is valid syntax. Is there a plugin or something I can click on (after making a text selection, maybe?) that will automatically do this? I am having to copy data to Excel and concatenate a comma to my lists and then copy them back to SQL Developer at the moment.
If your version of SQL Developer supports regex find and replace, then you can try the following from the find dialog (with the regex checkbox selected):
Find:
^([0-9])+$
Replace:
$1,
This should hit every line which has a solitary number on it (i.e. what you seem to want to target), and replace it with itself followed by a single comma. Note that I don't have SQL Developer in front of me; I've assumed the first capture group is accessible with $1, but it could be \1 or \\1 as well.
I see you have tag: visual-studio. In case you work with visual vs SQL Developer, I usually do it by follow this step.
copy to visual.
Keep Alt + left and move mouse from top to bottom.
Pus comma "," to line
Copy it to SQL Developer again.
The second way, I can do it by: Ctr + Find, and replace 1000 by ,1000
Edit > Block Selection
Then click and drag (or shift + down/up arrow) to create a multi-line cursor.

Delphi Adoquery SQL add or text

I'm trying to update my database in Delphi, but I'm not getting it right.
What I want is simple. This is my code:
form1.ADOQuery1.SQL.Clear;
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' +Form1.Edit4.Text);
form1.ADOQuery1.ExecSQL;
So basically, what I want is the Text written in the Edit to go into my database with the UPDATE function, where my database table is table1 and the field is named mark.
There is not enough information in your question to provide a definitive answer. However, I can make an estimated guess.
What you have shown would only work successfully if mark is defined as an ordinal or boolean field, and the user is entering appropriate numeric/boolean values into the TEdit.
But, if the mark field is defined as a textual field instead, you need to wrap the Text value in quote characters, otherwise you will produce invalid SQL syntax.
Imagine you entered a Text value of 'hello world'. Your original SQL statement would end up being the following, which is invalid syntax:
Update Table1 set mark=hello world
You need to wrap text values in quote characters instead:
Update Table1 set mark='hello world'
Or:
Update Table1 set mark="hello world"
For example:
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' + QuotedStr(Form1.Edit4.Text));
Or:
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' + AnsiQuotedStr(Form1.Edit4.Text, #34));
It is important to use a function like (Ansi)QuotedStr() to avoid SQL injection attacks. This is done by ensuring any embedded quote characters in the input text are escaped property. Otherwise, if you just did something like this instead:
form1.ADOQuery1.SQL.Add('Update Table1 set mark="' + Form1.Edit4.Text + '"');
The user could enter a text value like '"; <arbitrary SQL here>' and really reek havoc with your database.
The safer approach is to use a parameterized query instead, and let ADO handle any necessary SQL formatting for you (make sure TADOQuery.ParamCheck is true):
form1.ADOQuery1.SQL.Clear;
form1.ADOQuery1.SQL.Add('Update Table1 set mark=:Mark');
form1.ADOQuery1.Parameters.ParamByName('Mark').Value := Form1.Edit4.Text;
form1.ADOQuery1.ExecSQL;

MS SQL statement using PARTIAL and DISTINCT in VB.NET

I have a table called Postcodes which has over half a million records.
Inside this table i have a column called PostCodeText which has the following:
NG1 1AA
NG1 1AB
NG1 1AC
NG2 1AA
NG2 5TH
NG17 3LP
DE15 4BP
NG17 5GL
DE19 4EE...
What I need is a MSSQL statement that return DISTINCT matches based on a partial string. For example: If I wanted to find all distinct NG postcodes I would want to return:
NG1
NG2
NG17
I've tried something like:
SELECT DISTINCT postcodetext
FROM postcodes
WHERE (postcode_text LIKE 'NG%')
ORDER BY postcodetext
I feel I might be close to the answer but its not there yet, any help would be much appricated.
Also I heard that using LIKE is a slower option then using = is there a faster way then doing this?
Try
SELECT LEFT(postcodetext,CHARINDEX(' ',postcodetext,0))
FROM postcodes
WHERE postcodetext like 'NG%'
GROUP BY LEFT(postcodetext,CHARINDEX(' ',postcodetext,0))
ORDER BY postcodetext
OK, I think I'm missing something here, the above statement work fine (if I drop the ORDER BY), when I run it in management studio. But when I run it in my VB.net code I'm having an issue:
After running the statement and assigning the values into my datatable, I then use the following code :
For Each rows In dt.Rows
Select Case opt
Case 1 '
frmSettings.lstAllPostcodes.Items.Add(rows.Item("postcodetext"))
frmSettings.ProgBarSettings.Value = i
i = i + 1
End Select
Next
It fails when trying to reference rows.item("postcodetext"). I realise that has something to do with the SELECT statement using the LEFT of. I would like my listbox to hold the partial results (ie NG1, NG2, NG17 etc etc)

parameter in sql query :SSRS

I am using oracleclient provider. I was wondering how do I use a parameter in the query.
select * from table A where A.a in ( parameter).
The parameter should be a multivalue parameter.
how do I create a data set?
Simple. Add the parameter to the report and make sure to check it off as multi-valued. Then in the data tab and go in and edit the query click the "..." button to edit the dataset. Under the parameters tab create a mapping parameter so it looks something like this (obviously you will have different names for your parameters):
#ids | =Parameters!ContractorIDS.Value
Then in the query tab use the coorelated sub-query like your example above. I have done this many times with SQL server and there is no reason it should not work with Oracle since SSRS is going to build an ANSI compliant SQL statement which it will pass to Oracle.
where A.myfield in (#ids)
You can't have a variable in list in oracle directly. You can however, break apart a comma seperated list into rows that can be used in your subquery. The string txt can be replaced by any number of values seperated by a comma.
select * from a where a.a in (
SELECT regexp_substr(txt,'[^,]+',1,level)
FROM (SELECT 'hello,world,hi,there' txt -- replace with parameter
FROM DUAL)
CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (txt, '[^,]'))+1
)
The query works by first counting the number of commas that are in the text string. It does this by using a reqular expression to remove all non commas and then counts the length of the remainder.
It then uses an Oracle "trick" to return that number + 1 number of rows from the dual table. It then uses the regexp_substr function to pull out each occurence.
Firstly in SSRS with an Oracle OLEDB connection you need to use the colon, not the # symbol e.g. :parameter not #parameter but then you aren't able to do this as a multi-valued parameter, it only accepts single values. Worse, if you are using an ODBC connection you have to use the question mark by itself e.g. ? not #parameter and then the ordering of parameters becomes important, and they also cannot be multi-valued. The only ways you are left with is using an expression to construct a query (join() function for the param) or calling a stored proc.
The stored proc option is best because the SSRS can handle the parameters for stored procs to both SQL Server and Oracle very cleanly, but if that is not an option you can use this expression:
="select column1, column2, a from table A where A.a in (" + Join(Parameters!parameter.Value,", ") + ")"
Or if the parameter values are strings which need apostrophes around them:
="select column1, column2, a from table A where A.a in ('" + Join(Parameters!parameter.Value,"', '") + "')"
When you right-click on the dataset, you can select "dataset properties" and then use the fx button to edit the query as an expression, rather than using the query designer which won't let you edit it as an expression.
This expression method is limited to a maximum limit of about 1000 values but if you have that many this is the wrong way to do it anyway, you'd rather join to a table.
I don't think you can use a parameter in such a situation.
(Unless oracle and the language you're using supports array-type parameters ? )
The parameters in oracle are defined as ":parametername", so in your query you should use something like:
select * from table A where value in (:parametername)
Add the parameter to the paramaters folders in the report and mark the checkbox "Allow multiple values".
As Victor Grimaldo mentioned… below worked for me very fine. As soon as I use the :parameter in my SQL query in SSRS dataset1.. it asked me to enter the values for these parameters, for which I choose already created SSRS parameters.
SELECT * FROM table a WHERE VALUE IN (**:parametername**)
Thanks Victor.

Resources