Wikia

Vim Tips Wiki

Changes: Execute sybase-sql queries and see the result in a split window

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
Line 19: Line 19:
   
 
<pre>
 
<pre>
au FileType sql map &lt;F12&gt; &lt;C-W&gt;&lt;C-O&gt;:silent w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword &gt; tmpsqlresult.txt&lt;CR&gt;:split tmpsqlresult.txt&lt;CR&gt;
+
au FileType sql map <F12> <C-W><C-O>:silent w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > tmpsqlresult.txt<CR>:split tmpsqlresult.txt<CR>
 
</pre>
 
</pre>
   
Line 38: Line 38:
   
 
<pre>
 
<pre>
au FileType sql map &lt;F12&gt; :let fname = tempname()&lt;CR&gt;&lt;C-W&gt;&lt;C-O&gt;:silent exe "w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword &gt; ". fname&lt;CR&gt;:exe "split " . fname&lt;CR&gt;
+
au FileType sql map <F12> :let fname = tempname()<CR><C-W><C-O>:silent exe "w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > ". fname<CR>:exe "split " . fname<CR>
 
</pre>
 
</pre>
   
Line 47: Line 47:
   
 
<pre>
 
<pre>
au FileType sql map &lt;C-F12&gt; &lt;ESC&gt;:let fname = tempname()&lt;CR&gt;&lt;C-W&gt;&lt;C-O&gt;:silent exe "'&lt;,'&gt;w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword &gt; ". fname&lt;CR&gt;:exe "split " . fname&lt;CR&gt;
+
au FileType sql map <C-F12> <Esc>:let fname = tempname()<CR><C-W><C-O>:silent exe "'<,'>w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > ". fname<CR>:exe "split " . fname<CR>
 
</pre>
 
</pre>
   
Line 56: Line 56:
   
 
<pre>
 
<pre>
:new&lt;CR&gt;:silent 0r !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword&lt;CR&gt;
+
:new<CR>:silent 0r !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword<CR>
 
</pre>
 
</pre>
   

Revision as of 23:36, September 29, 2008

Tip 958 Printable Monobook Previous Next

created July 8, 2005 · complexity intermediate · author Maxim Kim · version 6.0


I was killed by Sybase's query tool - SQL Advantage. It's editor is so awful that I can't use it at all.

So I decided to engage Vim and sybase database. (I think in that manner you can use Oracle or MS SQL-Server).

First of all, you have to have isql properly set up. And here is my "magic" key bind:

au FileType sql map <F12> <C-W><C-O>:silent w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > tmpsqlresult.txt<CR>:split tmpsqlresult.txt<CR>

It closes windows other than your sql one. Then send current buffer to stdin of isql, which direct its output to a file, which we split then.

Do not forget to provide your own server settings.

If you want split window to appear below your sql window add

set splitbelow

to your vimrc.

Comments

If you don't want to use tmpsqlresult.txt file then try this

au FileType sql map <F12> :let fname = tempname()<CR><C-W><C-O>:silent exe "w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > ". fname<CR>:exe "split " . fname<CR>

It uses temporary files. They will be automatically deleted after quiting Vim.


This allows to execute only selected statements:

au FileType sql map <C-F12> <Esc>:let fname = tempname()<CR><C-W><C-O>:silent exe "'<,'>w !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword > ". fname<CR>:exe "split " . fname<CR>

Mapped to Ctrl-F12.


And if you want reduce the length of the macro and let Vim clean up the temporary file for you, use

:new<CR>:silent 0r !isql -SYourServerName -DYourDatabaseName -UYourUserName -PYourPassword<CR>

A better solution is to use the excellent dbext script script#356.

Not only does it allow you to execute arbitrary sql, it has lots of helper functions such as lists of all the objects in the syste.


Around Wikia's network

Random Wiki