我试图从一个表中删除一行,并将一些额外的数据插入另一个表.我知道这可以在两个单独的命令中完成,一个用于删除,另一个用于插入新表.但是我试图将它们组合起来并且它不起作用,这是我目前的查询:
insert into b (one,two,num) values delete from a where id = 1 returning one, two, 5;
运行时,我收到以下错误:
错误:"删除"或附近的语法错误
任何人都可以指出如何实现这一目标,还是有更好的方法?还是不可能?
首先,我是pl/pgsql的新手.需要它用于项目.
我坚持这个(简化)问题.
我的db模式有一个到m的关系(作者,书籍,author_books)
现在我想要一个pl/psgsql函数insert_book.(我知道所有作者肯定已经在作者表中,所以我只想传递他们的主键).
这个功能大纲是我的想法.
create or replace function insert_book(book_to_insert book, authors integer[])
returns void as $$
begin
-- insert book into table books
-- for each author add an entry to author_books table
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
作为论据我想通过类型书的记录和编写它的作者.但这究竟是如何工作的呢?我用Google搜索了一下,似乎无法弄清楚这一点......
问题1:功能大纲是否"正确"/是否有意义?
问题2:如何将记录簿插入表格书?我是否必须遍历书籍的所有领域(title,isbn,publisher,...)并将它们添加到INSERT INTO语句中,还是有"更智能"的方式?
问题3:如何调用我的函数insert_book?我在这里找到了这个例子(http://dbaspot.com/postgresql/206142-passing-record-function-argument-pl-pgsql.html),但这对我没有帮助.出于测试目的,我使用shell,但稍后我们将使用Java和JDBC.
非常感谢您的帮助.
journeypost=# INSERT INTO user_data.user_data (username,randomint) VALUES ('mahman',1);
ERROR: array value must start with "{" or dimension information
LINE 1: ... user_data.user_data (username,randomint) VALUES ('mahman...
journeypost=# INSERT INTO user_data.user_data (username,randomint) VALUES {'mahman',1};
ERROR: syntax error at or near "{"
LINE 1: ...O user_data.user_data (username,randomint) VALUES {'mahman',...
journeypost=# INSERT INTO user_data.user_data (username,randomint) VALUES (2,{'mahman',1});
ERROR: syntax error at or near "{"
LINE 1: ...ser_data.user_data (username,randomint) VALUES (2,{'mahman',...
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL中,上述不同的INSERT语句失败了.我做错了什么?
编辑:
我的架构:
journeypost=# \dt user_data.*
List of relations
Schema | Name | Type | …Run Code Online (Sandbox Code Playgroud) 我使用postgresql和yii2框架.我收到了一条非常有趣的错误消息:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 1, null, null, null, null, 1, Demo, , , , 1998-01-01, , , , 345345435453453, , , , , 1, , , f, f, f, f, 10, f, 1, f, f, f, null, null, null, 1470477479, 1470477479, null).
Run Code Online (Sandbox Code Playgroud)
但我检查了我的插入命令,那里没有"id"列!
INSERT INTO "advertiser" ("languages", "type", "name", "display_name", "title", "about", "birthday", "gender", "country_id", "county_id", "city_id", "city_part", "street", "house_number", "phone", "public_email", "public_url", "motto", …Run Code Online (Sandbox Code Playgroud) 我知道有一个SQL命令是这样的:IF NOT EXISTS但是因为Android的SQLiteDatabase类有一些很好的方法,我想知道如果通过方法不存在它是否可以插入一个值.
目前我正在使用它来插入一个String:
public long insertString(String key, String value) {
ContentValues initialValues = new ContentValues();
initialValues.put(key, value);
return db.insert(DATABASE_TABLE, null, initialValues);
}
Run Code Online (Sandbox Code Playgroud)
(db是.的一个例子SQLiteDatabase.)
我尝试了这种方法insertOrThrow()而不是insert(),但它看起来和它一样insert().也就是说,如果该值已经在行中,则会再次插入该值,因此该行现在具有两个相同值的值.
我试图使用"OPENQUERY"从MS SQL Server将记录插入MySQL数据库,但我想要做的是忽略重复的密钥消息.因此,当查询遇到重复时,请忽略它并继续.
我可以做些什么来忽略重复?
这是我在做的事情:
问题是,当将表A连接到表B时,有时我会在表B中找到2条以上的记录,这些记录与我要查找的条件相匹配,这会导致我的数据集中的值A.record_id超过2倍,然后将其插入表A中这导致了问题.注意我可以使用聚合函数来消除记录.
我创建了一个名为resources的表,但是当我在其中插入值时,抛出此异常:
android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failedexception
Run Code Online (Sandbox Code Playgroud)
这是我的create table语句:
public static final String DATABASE_CREATE =
"CREATE TABLE " + table_resources + "(ID INTEGER PRIMARY KEY,
KEY_TYPE text, KEY_ENCODING text, KEY_WIDTH text, KEY_HEIGHT text,
KEY_DATA text, KeyIId text)";
Run Code Online (Sandbox Code Playgroud)
以下是我的插入代码:
JSONObject show = data.getJSONObject(i);
if (show.get("type").equals("resource_updates")) {
JSONArray resources = show.getJSONArray("resources");
try {
System.out.println("length of resources is is " + resources.length());
for (int resourceIndex = 0; resourceIndex < resources.length(); resourceIndex++) {
type = resources.getJSONObject(resourceIndex).getString("type").toString();
encoding = …Run Code Online (Sandbox Code Playgroud) REPLACE INTOMySQL中的函数以删除和插入行的方式工作.在我的表中,主键(id)是自动递增的,所以我期望它删除然后id在数据库的尾部插入一个表.
但是,它出乎意料并插入相同的id!这是预期的行为,还是我在这里遗漏了什么?(我id在调用REPLACE INTO语句时没有设置)
有没有办法可以使用硬值和子查询的组合来插入一个命令的表?
例如:
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name
FROM customers
WHERE city = 'San Diego';
Run Code Online (Sandbox Code Playgroud)
我需要supplier_type为3.所以我可以为第二行执行以下操作吗?
SELECT account_no, name, supplier_type = 3
Run Code Online (Sandbox Code Playgroud)
supplier_type不在customers表中
我有下表:
示例:
create table test
(
col1 varchar(10),
col2 varchar(20),
col3 varchar(30)
);
Run Code Online (Sandbox Code Playgroud)
现在我想按变量插入两个值,最后一个用#temp表插入.
#Temp:
create table #temp
(
col3 varchar(30)
);
Run Code Online (Sandbox Code Playgroud)
#Temp:包含
col3
-----
A1
A2
A3
Run Code Online (Sandbox Code Playgroud)
插入测试表:
Declare @col1 varchar(10) = 'A'
Declare @col1 varchar(20) = 'B'
Declare @sql varchar(max)
SET @SQL = N'insert into test values('+@col1+','+@col2+',........);
EXEC(@SQL)
/* How to insert `@col3` from #temp to test table*/
Run Code Online (Sandbox Code Playgroud)
预期成果:
col1 col2 col3
------------------
A B A1
A B A2
A B A3
Run Code Online (Sandbox Code Playgroud)
注意:变量值必须重复,直到#temp值插入到表测试中.
sql-insert ×10
postgresql ×4
sql ×4
android ×2
mysql ×2
sql-delete ×2
sql-server ×2
types ×2
arrays ×1
database ×1
duplicates ×1
java ×1
null ×1
openquery ×1
plpgsql ×1
yii ×1