How do I reset a sequence in Oracle?

Here is a good procedure for resetting any sequence to 0 from Oracle guru Tom Kyte. Great discussion on the pros and cons in the links below too. tkyte@TKYTE901.US.ORACLE.COM> create or replace procedure reset_seq( p_seq_name in varchar2 ) is l_val number; begin execute immediate ‘select ‘ || p_seq_name || ‘.nextval from dual’ INTO l_val; execute … Read more

Right query to get the current number of connections in a PostgreSQL DB

Those two requires aren’t equivalent. The equivalent version of the first one would be: SELECT sum(numbackends) FROM pg_stat_database; In that case, I would expect that version to be slightly faster than the second one, simply because it has fewer rows to count. But you are not likely going to be able to measure a difference. … Read more

Select SQL Server database size

Try this one – Query: SELECT database_name = DB_NAME(database_id) , log_size_mb = CAST(SUM(CASE WHEN type_desc=”LOG” THEN size END) * 8. / 1024 AS DECIMAL(8,2)) , row_size_mb = CAST(SUM(CASE WHEN type_desc=”ROWS” THEN size END) * 8. / 1024 AS DECIMAL(8,2)) , total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2)) FROM sys.master_files WITH(NOWAIT) WHERE database_id = … Read more

How to do an INNER JOIN on multiple columns

You can JOIN with the same table more than once by giving the joined tables an alias, as in the following example: SELECT airline, flt_no, fairport, tairport, depart, arrive, fare FROM flights INNER JOIN airports from_port ON (from_port.code = flights.fairport) INNER JOIN airports to_port ON (to_port.code = flights.tairport) WHERE from_port.code=”?” OR to_port.code=”?” OR airports.city=’?’ Note … Read more

Conversion failed when converting date and/or time from character string while inserting datetime

There are many formats supported by SQL Server – see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have – therefore, these settings might work some times – and sometimes not. The way to solve this is to use the (slightly adapted) ISO-8601 date format … Read more

What is Ad Hoc Query?

Ad hoc is latin for “for this purpose”. You might call it an “on the fly” query, or a “just so” query. It’s the kind of SQL query you just loosely type out where you need it var newSqlQuery = “SELECT * FROM table WHERE id = ” + myId; …which is an entirely different … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)