执行批处理文件时出现问题。 我的批处理文件到达执行postgres查询的行后退出
( echo INSERT INTO Role SET roleName=%role%; INSERT INTO User (login, password, active, role) VALUE (%login%, %password%, 1, LAST_INSERT_ID()); ) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%任何人都可以帮助解决这个问题吗?
@echo off
title Create New User
set DB_HOST=localhost
set DB_USER=postgres
set DB_NAME=appdb
@echo Enter a new Login:
set /p login=
ECHO ============================
@echo Enter a new password:
set /p password=
ECHO ============================
@echo Enter again your new password:
set /p passwordConfirm=
goto VerifyPassword
:VerifyPassword
if %password% == %passwordConfirm% goto insertInDatabase
if not %password% == %passwordConfirm% goto PasswordSection
:PasswordSection
@echo Password incorrect, please retype your password
@echo Enter a new password:
set /p password=
ECHO ============================
@echo Enter again your new password:
set /p passwordConfirm=
goto VerifyPassword
:insertInDatabase
ECHO ============================
@echo Choose role of the user Admin A\U User:
set /p role=
@echo Inserting in Database ...
(
echo INSERT INTO Role SET roleName=%role%; INSERT INTO User (login,
password, active, role) VALUE (%login%, %password%, 1, LAST_INSERT_ID());
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
pause
exit
答案 0 :(得分:0)
我认为有两个问题: 1.括号必须转义 2.您查询的语法有误。
尝试一下:
(
echo INSERT INTO role ^(Name^) select '%role%' where not exists ^(select 1 from role where lower^(name^) = lower^('%role%'^)^);
echo INSERT INTO User ^(login, password, active, role^) select '%login%', '%password%', true, r.id from role r where lower^(name^) = lower^('%role%'^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%