I have this line in my VB.net app:
Dim cmd2 As New SqlClient.SqlCommand("Select TOP 1 Record from [SRFeedData_Local].[dbo].[Record] where Record LIKE '%" & CRIS & "%'", con)
This works fine, but if I try and run this query from within SQL Server Management Studio like this:
select Record
from [SRFeedData_Local].[dbo].[Record]
where Record like '%" & 'SGWSM94VMW0000146' & "%'"
it complains about the syntax.
Why the difference ?
You have to use a + instead of &
Something like
select Record
from [SRFeedData_Local].[dbo].[Record]
where Record like '%' + 'SGWSM94VMW0000146' + '%'
Related
I have a simple insert statement I want to execute using sqlrpgle.
I build the statement first depending on what variables are empty and then want to use exec sql to do the insert.
This does not work.
But entering the same insert statement with subquery in STRSQL works fine.
I have broken down the sql statement to its simplest version for this question.
Here is the SQLRPGLE code snippet
And here is where I enter the same statement in STRSQL
Any Assistance would be greatly appreciated.
**EDIT
This only happens when I add a where clause, which is essentially what I need and why I am trying to do it using sqlrpgle.
Statements like
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'select shscdt, shcmdt ' +
'from r50files/sbschd ';
and
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'values (10, 10) ';
All work just fine, once a where clause is added to the subquery is where it does not work in the RPG code but in STRSQL
You have sqlstmt defined as 100 characters maximum and with the where clause your string is over 100 characters long. Your SQL statement is getting truncated and that's why its not working. I suspect you know how to fix this but just for completeness, the solution is the following:
D sqlstmt s 500a varying
I have a form which has a ComboBox on it that pulls all DISTINCT colleague names from a huge table that includes all of our sales (50k+ records). It works perfectly fine, but it takes 3-4 minutes to open the form because it takes so long for Access to find all unique colleague names in the table.
I've been trying to research this and found something that looks useful, but can't seem to get it right.
The code I have at the moment:
Private Sub CollName_Change()
Dim strText As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
Me.CollName.RowSource = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE ""*"" & strText & ""*""; "
Me.CollName.Dropdown
End If
End Sub
I found this code on two forums, this is supposed to do the following: "the key is to not have a Row Source defined for the Combo Box. The row source will be defined as the user starts typing letters. Once they get to 3 letters then the row source of the combo box will be defined and the combo box will be told to dropdown."
When I get to 3 letters, a dropdown appears, but it's blank, it doesn't display any results.
I'm relatively new to Access, although already built two databases, but they all have relatively basic SQL queries, so I have no idea what I'm not doing right here.
Any advice? Or alternatively a different solution as to how take my combo box faster and still keep values unique?
You only have some double-quote mixup there. It is much easier to use single quotes instead of double double-quotes.
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
But your query would be way faster if you would only use the starting letters, i.e. remove the leading *
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '" & strText & "*';"
But that depends on your requirements.
EDIT to debug:
Dim strText As String
Dim strSelect As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
strSelect = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
Debug.Print strSelect
Me.CollName.RowSource = strSelect
Me.CollName.Dropdown
End If
Copy strSelect from the Immediate Window into a query, and try it out. It should help resolve the problem.
See How to debug dynamic SQL in VBA
I am trying to save query results to text file automatically, without looping through reader object in VB.NET with using ODBC windows connection.
But I can't find how!
That's what I try so far:
mCmd = New OdbcCommand( _
"SELECT my_id FROM " & myTable & " WHERE myflag='1' \o 'c:/a_result.txt'", mCon)
n = mCmd.ExecuteNonQuery
But that don't work at all.
Please advice or code example how to get it.
And second...
It will be ideally that with saving results to text I get a number of saved rows in variable 'n'.
As for now I get only 0 or 1 depends if query was successful or not.
EDIT:
After some fighting I found a way for do this with more or less success.
To txt file:
mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.txt' DELIMITER AS '|'", mCon)
To csv file:
mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.csv' WITH CSV", mCon)
That works, but I am not able to escape quotes and '\' so I got double signs in output file.
If someone with experience know how to achieve escaping and changing delimiter for csv files I would be glad to see it on given example.
Variable 'n' after query contain a number of exported rows.
The \o sequence is a psql meta-command. That means it is a feature of psql. If you want this functionnality you will have to implement it in your client. It is very easy though.
Was trying to datestamp the output filename - but keep getting errors -- along the lines of:
Select * from Orders
output to 'c:'+ select (CONVERT(varchar(10), GETDATE(), 120)) + 'orders.csv'
Any help appreciated...
I had similar problem on Sybase 9 - I had to write each procedure / function body inside spearate file named as that procedure. So I had to dynamically create file name using name of each procedure (or function). Below is my solution (works in isql):
begin
declare folder varchar (40);
declare fileName varchar(60);
-- folder parameter is our destination path - the 4 backslashes are important here
set folder = 'c:\\\\test\\\\sql\\\\';
-- here we are iterating over all procedures / functions
for p as curs dynamic scroll cursor for
Select distinct sysobjects.name
from sysobjects inner join syscomments
on sysobjects.id = syscomments.id where sysobjects.type = 'P'
do
-- each procedure must be inside separate file named like that procedure
set fileName = folder + name + '.sql';
-- finally, there are answer to original question:
-- we are exporting data into file, whose name is defined dynamically
unload select proc_defn from SYS.SYSPROCEDURE where proc_name = name
to fileName escapes off;
end for;
end
output to is a dbisql command, so it's interpreted on the client. This means that you can't use expressions for the filename, since they are executed on the server. However you can use the unload select statement (which does run on the server) with the into client file clause to do what you want.
See docs on the unload select statement here.
Disclaimer: I work for Sybase in SQL Anywhere engineering.
I have two strings in SQL and the REPLACE function only works on one of them, why is that?
Example 1:
SELECT REPLACE('18 286.74', ' ', '')
Example 2:
SELECT REPLACE('z z', ' ', '')
Example 1's output is still "18 286.74" whereas Example 2's output is "zz". Why does SQL not react the same way to both strings?
UPDATE:
When running select replace('123 123.12', ' ', '') that works fine, still not with '18 286.74'.
Test it the following way.
select unicode(substring('18 286.74', 3, 1))
If the code returns 32 then it's a space, if not, it's a different Unicode character and your replace ' ' won't work.
maybe cast is needed.
UPD: or not(on sql 2005 works fine too)
Are you sure it is a space? i.e. the same whitespace character that you are passing as the second argument? The code you've posted works fine for me on SQL Server 2008.
Re working on your friends PC - perhaps the whitespace got normalized when you sent it to him?
You are probably using non-breakable space.
I could reproduce it by typing ALT+0160 into the number in SELECT REPLACE('18 286.74', ' ', '')
Could you please issue this following:
SELECT CAST('18 286.74' AS BINARY), REPLACE('18 286.74', ' ', '')
by copying the '18 286.74' from REPLACE into CAST?
I was having the same issue and found that it was a char(10) (line feed). when copied out of Managment Studio it became a char(32) but in the record it was a char(10) try
Select Replace(#string, char(13), '')