Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLCMD use LIKE '%@%'
    primarykey
    data
    text
    <p>I'm trying to run a query using SQLCMD.EXE and have trouble with the LIKE portion.</p> <pre><code>WHERE email LIKE '%%@%%' </code></pre> <p>I think it is an error with cmd prompt rather then SQLCMD.EXE since I get the error:</p> <pre><code>Syntax error "@%'" </code></pre> <p>I am running this via Notepad++ (NppExec) pointing to the bat file like so:</p> <pre><code>H:\scripts\SQL.bat "$(CURRENT_WORD)" </code></pre> <p>This causes the query to be wrapped in double quotes before being used by the SQLCMD.EXE call. The SQLCMD.EXE call then runs in the bat file like so:</p> <pre><code>SQLCMD.EXE -U user -P %pass% -S %server% -Q %sql% -d %table% </code></pre> <p>It works perfect on any query I use aside from this <code>LIKE '%%@%%'</code> part.</p> <p><strong>UPDATE</strong></p> <p>I've done a few more tests and think I have narrowed it down to being a problem with the <code>%</code> and the <code>@</code>.</p> <p>So queries like these work fine:</p> <ul> <li><code>SELECT name FROM table WHERE name LIKE 'test'</code></li> <li><code>SELECT name FROM table WHERE name LIKE 'test%'</code></li> <li><code>SELECT name FROM table WHERE name LIKE '%%test'</code></li> </ul> <p>But these will cause errors:</p> <ul> <li><code>SELECT name FROM table WHERE name LIKE '%test'</code></li> <li><code>SELECT name FROM table WHERE name LIKE '%test%'</code></li> </ul> <p>This is fine since I am ok with doubling the <code>%</code> in my queries, but I've tried <code>%%@%</code> and <code>%%@%%</code> and they throw errors. <code>Syntax error "@'""</code> or <code>Syntax error "@%'""</code>, respectively.</p> <p>Also, the reason for the variables is that I included some logic so it can detect table names and run for different servers and databases.</p> <p>Here is the bat file</p> <pre><code>set sql=%1 iff %@index[%sql%,sur_] GT -1 THEN SET SERVER=server1 SET table=tablename SET pass=password else SET SERVER=server2 SET table=tablename SET pass=password endiff SQLCMD.EXE -U usr -P %pass% -S %server% -Q %sql% -d %table% </code></pre> <p>The reason for the weird syntax is due to the command being run through TCC/LE <a href="http://jpsoft.com/help/index.htm" rel="nofollow">(see here)</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload