SQL String error? No Such Column - c++

I'm using sqlite3 in my c++ program and am trying to run an SQL string in the
sqlite3_get_table function.
Here's my sql string.
SELECT name FROM sqlite_master WHERE type='table' AND name=test_table;
I keep getting the error "no such column "test_table"" .
All I am trying to do is confirm the existence of a table in my database. That's all.
Any clues as to what's wrong with my string.

In SQLite double quotes ('"') is the identifier-escape character, so assuming this is your SQL (raw SQL, nothing to do with C++):
SELECT
name
FROM
sqlite_master
WHERE
type = 'table'
AND
name = "test_table;"
Is equivalent to:
...
name = test_table
...which obviously isn't what you want.
You should use single-quoted strings in SQL, and the statement-terminating semicolon should go at the very end:
SELECT
name
FROM
sqlite_master
WHERE
type = 'table'
AND
name = 'test_table';

Related

How to include table name in result for wildcard query?

I'm querying from a bunch of tables in bigquery using a wildcard query. I'd like each result row to show which table it's from.
I've tried to include _TABLE_SUFFIX in the select, but it won't compile:
SELECT _TABLE_SUFFIX, *
FROM `foo.bar_*`
WHERE x = ...
Invalid field name "_TABLE_SUFFIX"
use alias as in below example
SELECT _TABLE_SUFFIX as table_name, *
FROM `foo.bar_*`
WHERE x = ...
You can also use below to preserve full table name
SELECT CONCAT('bar_', _TABLE_SUFFIX) as table_name, *
FROM `foo.bar_*`
WHERE x = ...
Note also: Field names are not allowed to start with the (case-insensitive) prefixes _PARTITION, _TABLE_, _FILE_ and _ROW_TIMESTAMP

Referencing a macro variable created by a prompt SAS EG

I created a prompt in SAS EG that takes a text input and creates the macro variable called 'variableName'.
I am trying to reference this macro variable like so:
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = &variableName ;
This gives me an error that says: "Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER."
I have also tried enclosing &variableName in single and double quotes but when I do that I just don't get any results.
I am able to reference the prompt when I use query builder and filter data based on the prompt, but I am trying to use the prompt's value in calculated expressions, etc. and in queries I write without query builder. How can i reference the variable I created in the prompt??
Edit: code with a value that the macro variable would have
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = 'NAME OF PERSON';
When I run that, I get the results I want.
It needs to resolve to valid SAS code. Assuming &variableName is a string, then it would be something like:
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = "&variableName." ;
If this isn't working, please show a query that does work with the same value as the macro variable would have. And then we can suggest how to change your code.
Edit: based on your comment you do not have the prompt connected to your query. Right click the query and link the prompt to your query and it will run before the query to provide the value.

Drop if exists in netezza

I need a command to delete a table if that exists in NETEZZA, Something like that:
drop table if exists xxx;
I have searched and tried many but it didn't work. Can you help me here?
In netezza you can use this syntax:
drop table table_name if exists;
There is nothing built-in, but you can create a stored proc which uses the catalog views to check if the table exists before attempting to drop it:
create or replace procedure maybe_drop(varchar(128))
returns boolean
language nzplsql
as
begin_proc
declare
oname alias for $1;
o record;
begin
select otype into o
from (
select 'TABLE' otype from _v_table where tablename = upper(oname)
union all
select 'VIEW' otype from _v_view where viewname = upper(oname)
) x;
if found then
execute immediate 'DROP '||o.otype||' '||oname;
end if;
end;
end_proc;

SOCI Cannot prepare statement

I have a function like this:
CREATE OR REPLACE FUNCTION get_path_set_1(IN pathset_id_in character varying, OUT id character varying, OUT pathset_id character varying, OUT utility double precision)
RETURNS SETOF record AS
$BODY$
begin
if exists(SELECT 1 FROM "PathSet_Scaled_HITS_distinctODs" WHERE "ID" = $1) then
return query SELECT "ID", "PATHSET_ID", "UTILITY"
FROM "SinglePath_Scaled_HITS_distinctODs"
where "PATHSET_ID" = $1;
end if;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION get_path_set_1(character varying)
OWNER TO postgres;
when I call it in my program using this:
std::string testStr("43046,75502");// or std::string testStr("'43046,75502'");
soci::rowset<sim_mob::SinglePath> rs = (sql.prepare << "get_path_set_1(:pathset_id_in)",soci::use(testStr));
I get the following exception:
terminate called after throwing an instance of 'soci::postgresql_soci_error'
what(): Cannot prepare statement. ERROR: syntax error at or near "get_path_set_1"
LINE 1: get_path_set_1($1)
I will appreciate if you help me detect missing part
thank you
This does not solve the error you report. But simplify your function:
CREATE OR REPLACE FUNCTION get_path_set_1(pathset_id_in varchar)
RETURNS TABLE(id varchar, pathset_id varchar, utility double precision) AS
$func$
BEGIN
RETURN QUERY
SELECT "ID", "PATHSET_ID", "UTILITY"
FROM "SinglePath_Scaled_HITS_distinctODs"
WHERE "PATHSET_ID" = $1;
END
$func$ LANGUAGE plpgsql;
RETURNS TABLE is the modern, more elegant, equivalent form of the combination RETURNS SETOF record and OUT parameters.
IF exists ... is buying you nothing here. Run the query; if nothing is found, nothing is returned. Same result for half the cost.
From this piece of code:
soci::rowset<sim_mob::SinglePath> rs =
(sql.prepare << "get_path_set_1(:pathset_id_in)",soci::use(testStr));
it appears you're trying to prepare a query that just contains the function call without even a SELECT.
That's not valid in SQL. You want to prepare this query instead:
SELECT * FROM get_path_set_1(:pathset_id_in)
This form (select * from function(...)) is also necessary because the function returns a resultset with multiple columns, as opposed to just a scalar value.
Also as Erwin mentions, the OUT and SETOF RECORD are weird in this case, I'll second his advice on using RETURNS TABLE.

Nested statements in sqlite

I'm using the sqlite3 library in c++ to query the database from *.sqlite file. can you write a query statement in sqlite3 like:
char* sql = "select name from table id = (select full_name from second_table where column = 4);"
The second statement should return an id to complete the query statement with first statement.
Yes you can, just make sure that the nested query doesn't return more than one row. Add a LIMIT 1 to the end of the nested query to fix this. Also make sure that it always returns a row, or else the main query will not work.
If you want to match several rows in the nested query, then you can use either IN, like so:
char* sql = "select name from table WHERE id IN (select full_name from second_table where column = 4);"
or you can use JOIN:
char* sql = "select name from table JOIN second_table ON table.id = second_table.full_name WHERE second_table.column = 4"
Note that the IN method can be very slow, and that JOIN can be very fast, if you index on the right columns
On a sidenote, you can use SQLiteadmin (http://sqliteadmin.orbmu2k.de/) to view the database and make queries directly in it (useful for testing etc).