Big Query Concat String in FROM Clause - sql

I have multiple tables like as follows in BigQuery:
PROJECT_NAME.DATA_SET_NAME.TABLENAME0
PROJECT_NAME.DATA_SET_NAME.TABLENAME1
PROJECT_NAME.DATA_SET_NAME.TABLENAME2
PROJECT_NAME.DATA_SET_NAME.TABLENAME3
PROJECT_NAME.DATA_SET_NAME.TABLENAME4
...
I want to empty some of these tables via a loop but don't know how to call the CONCAT in FROM clause:
DECLARE count INT64 DEFAULT 0;
WHILE count < 1000 Do
DELETE FROM CONCAT('PROJECT_NAME.DATA_SET_NAME.TABLENAME' , count ) WHERE TRUE;
SET count = count + 1;
END WHILE
But it's not working, it says I cannot use CONCAT in FROM part.
Anyone knows how should I do it?
Thanks

This is not currently possible in BigQuery, unless you script it outside BigQuery.
There's an open feature request, that you should subscribe to - to indicate interest and follow any new developments:
https://issuetracker.google.com/issues/142531516

Related

UPDATE QUERY - Sum up a value from form with value from table

I've just started using microsoft access so I don't really know how to solve this. I would like to use an update query to add a value from a form to a value on a table.
I originally used the SUM expression which gave me an error saying it was an aggregate function.
I also tried to add the two values together (e.g [field1] + [field2]) which as a result gave me a value with both numbers together instead of adding them together.
The following is the SQL I'm using:
UPDATE Votes
SET Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE (((Votes.ActID) = [Forms]![frmVote]![combacts])
AND ((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
I want to add a value [txtnumvotes] a form to a field [NumVotes] from the table [Votes].
Could someone please help me?
You can specify the expected data type with parameters:
PARAMETERS
[Forms]![frmVote]![txtnumvotes] Short,
[Forms]![frmVote]![combacts] Long,
[Forms]![frmVote]![combrndnum] Long;
UPDATE
Votes
SET
Votes.NumVotes = [Votes]![NumVotes]+[Forms]![frmVote]![txtnumvotes]
WHERE
(((Votes.ActID) = [Forms]![frmVote]![combacts])
AND
((Votes.RoundNum) = [Forms]![frmVote]![combrndnum]))
Without the specification, Access has to guess, and that sometimes fails.

Rails Active Record: SQL Expression In Update Statement

How can you execute the following SQL statement using AR:
UPDATE accounts SET balance = balance + 100 WHERE id = 1
I tried to find ways to execute SQL expressions in AR update statements, but nothing is working. I can not do something like
account.update balance: account.balance + 100
Because that would open all sort of race condition issues. The DBMS is the proper entity to handle this kind of update.
Any idea how to write it using AR?
I realize this can be done using:
Account.where(id: 1).update_all("balance = balance + 100")
But this looks very un-rails like to me. I was hoping for a cleaner solution
You need to do:
account.increment :balance, 100
increment is the documentation.

num_rows in postgres always return 1

I'm trying to do a SELECT COUNT(*) with Postgres.
What I need: Catch the rows affected by the query. It's a school system. If the student is not registered, do something (if).
What I tried:
$query = pg_query("SELECT COUNT(*) FROM inscritossimulado
WHERE codigo_da_escola = '".$CodEscola."'
AND codigo_do_simulado = '".$simulado."'
AND codigo_do_aluno = '".$aluno."'");
if(pg_num_rows($query) == 0)
{
echo "Error you're not registered!";
}
else
{
echo "Hello!";
}
Note: The student in question IS NOT REGISTERED, but the result is always 1 and not 0.
For some reason, when I "show" the query, the result is: "Resource id #21". But, I look many times in the table, and the user is not there.
You are counting the number of rows in the answer, and your query always returns a single line.
Your query says: return one row giving the number of students matching my criteria. If no one matches, you will get back one row with the value 0. If you have 7 people matching, you will get back one row with the value 7.
If you change your query to select * from ... you will get the right answer from pg_num_rows().
Actually, don't count at all. You don't need the count. Just check for existence, which is proven if a single row qualifies:
$query = pg_query(
'SELECT 1
FROM inscritossimulado
WHERE codigo_da_escola = $$' . $CodEscola . '$$
AND codigo_do_simulado = $$' . $simulado. '$$
AND codigo_do_aluno = $$' . $aluno . '$$
LIMIT 1');
Returns 1 row if found, else no row.
Using dollar-quoting in the SQL code, so we can use the safer and faster single quotes in PHP (I presume).
The problem with the aggregate function count() (besides being more expensive) is that it always returns a row - with the value 0 if no rows qualify.
But this still stinks. Don't use string concatenation, which is an open invitation for SQL injection. Rather use prepared statements ... Check out PDO ...

Using Sqlite3 in objective C How to Retrieve the number of rows in a result set

after a long search didn't succeed to get the required and easily understandable answer i am putting here a question…..
so please help me out on it….
I just want know that how i can get the number of rows in a result set of a query in objective C using sqlite3.
as we just use the function of SQL Server in PHP.
$Query = "SELECT XXXX FROM XXX";
$rstRow = Sql_Query($Query);
if (sqlsrv_num_rows($rstRow) > 0)
{
/* do something */
}
what is alternate to this in sqlite3.
Sqlite does not provide any function to get the number of rows return by a query. You can use SELECT COUNT(*) FROM table_name to get total number rows.
Reference: link

Yii Framework: How to get the num_rows?

As the official documentation does not say how to do a simply "num_rows" with their system, i need some help here: How to get the amount of rows in the result set ?
Assuming:
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
This will work for insert, update and delete:
$rowCount=$command->execute();
execute(): performs a non-query SQL statement, such as INSERT, UPDATE and DELETE. If successful, it returns the number of rows that are affected by the execution.
For select, you could do the following:
$dataReader=$command->query();
This generates the CDbDataReader instance and CDbDataReader provides a rowCount property
http://www.yiiframework.com/doc/api/1.1/CDbDataReader#rowCount-detail
$rowCount = $dataReader->rowCount;
About rowCount => Returns the number of rows in the result set. Note, most DBMS may not give a meaningful count. In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
ActiveRecord has count method which can be used.
$cntCriteria = new CDbCriteria();
$cntCriteria->condition = "categoryId = :categoryId";
$cntCriteria->params[':categoryId'] = $categoryRow->categoryId;
$articleCount = Article::model()->count($cntCriteria);
There is one more way to do this. When we execute a sql query it will return the result as array only. So we can able get the count of the rows using count() function like below.
$output=User::model()->findAllBySql("select * from user");//User is a model belongs to the user table
$count_val=count($output);//$count_val has the value of number of rows in the output.