Question

I have this in the .sql file in my buffer:

 CREATE TABLE WH.dbo.customer(
        id              INTEGER NOT NULL,
        cust_name       VARCHAR(30) NOT NULL,
        phone_nbr       VARCHAR(30) NULL,
        PRIMARY KEY(id)
    );

If I am in normal mode and the cursor is on TABLE then I sould be able to hit either <Leader>se or command gVim to :DBExecSQLUnderCursor and the statement should be executed as dbext should find CREATE and ; and then execute the script in between. But i get the following message:

Last SQL:
CREATE TABLE WHAnalysis.dbo.customer(

If I highlight all the script and choose Execute SQL (Visual selection) from the plugin menu then it runs fine.

What is going on? Could it be a setting in my _vimrc?:

set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()

" Use CTRL-S for saving, also in Insert mode
:nnoremap <C-S>     :<C-U>update<CR>
:vnoremap <C-S>     :<C-U>update<CR>gv
:cnoremap <C-S>     <C-C>:update<CR>
:inoremap <C-S>     <C-O>:update<CR>

" Microsoft SQL Server
let g:dbext_default_profile_WH = 'type=SQLSRV:user=dbuser:passwd=dbuserpassword:dsnname=SQLOLEDB.1:srvname=dwdb'

set nocp
call pathogen#infect()
syntax on
filetype plugin indent on

"Mouse and backspace
set mouse=a

function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let eq = ''
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      let cmd = '""' . $VIMRUNTIME . '\diff"'
      let eq = '"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction

nnoremap <Leader>p :pyf P:\Computer Applications\Python\

"quick quit command
noremap <Leader>e :quit<CR> "quits the current window



" Rebind <Leader> key
let mapleader = ","

map <Leader>n <esc>:tabprevious<CR>
map <Leader>m <esc>:tabnext<CR
Was it helpful?

Solution

I spoke to the plugin maintainer David Fishburn - I was amazed at the time-out he took to help a novice like me: great guy.

Initially he suggested

I believe the cmd terminator for SQLSRV is "\ngo\n" not ";".

If you want to change it temporarily to try in this buffer run: :DBSetOption cmd_terminator=';'

Try the cmd again.

If that works you can either override the default or change your profile to override it.

Then in answer to some further related questions:

Q1. What is "\ngo\n" ?

Because the string is enclosed in double quotes, Vim treats escaped characters differently. \n - newline go \n - newline

So for SQL Server this would be typical:

CREATE PROCEDURE BEGIN END go

Which is actually:
"END\ngo\n"

In other words, "go" has to be on a newline, with only "go" on the line.

Q2. Do I just add the following to _vimrc for it to become permanent?:

DBSetOption cmd_terminator=';'

No. :DBSetOption is used to modify current buffer settings only, not permanent settings.

The best thing you can do is read through :h dbext.txt.

The specific answer to your question lies in :h dbext-configure-options

5.3 Database Specific Options dbext-configure-options The command terminator is automatically added to a command before it is sent to the database. The command options are also added to the command line used to execute the statement. >

    dbext_default_SQLSRV_bin               = "osql"
    dbext_default_SQLSRV_cmd_header        = ""
    dbext_default_SQLSRV_cmd_terminator    = "\ngo\n"
    dbext_default_SQLSRV_cmd_options       = '-w 10000 -r -b -n'
    dbext_default_SQLSRV_extra             = ''

So, to permanently override for all buffers in your .vimrc you would add:
let g:dbext_default_SQLSRV_cmd_terminator = ";"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top