SQL Server: Difference between PARTITION BY and GROUP BY

They’re used in different places. group by modifies the entire query, like: select customerId, count(*) as orderCount from Orders group by customerId But partition by just works on a window function, like row_number: select row_number() over (partition by customerId order by orderId) as OrderNumberForThisCustomer from Orders A group by normally reduces the number of rows … Read more

How to do version control for SQL Server database?

Martin Fowler wrote my favorite article on the subject, http://martinfowler.com/articles/evodb.html. I choose not to put schema dumps in under version control as alumb and others suggest because I want an easy way to upgrade my production database. For a web application where I’ll have a single production database instance, I use two techniques: Database Upgrade … Read more

Hidden Features of SQL Server

In Management Studio, you can put a number after a GO end-of-batch marker to cause the batch to be repeated that number of times: PRINT ‘X’ GO 10 Will print ‘X’ 10 times. This can save you from tedious copy/pasting when doing repetitive stuff.

Oracle: is there a tool to trace queries, like Profiler for sql server? [closed]

I found an easy solution Step1. connect to DB with an admin user using PLSQL or sqldeveloper or any other query interface Step2. run the script bellow; in the S.SQL_TEXT column, you will see the executed queries SELECT S.LAST_ACTIVE_TIME, S.MODULE, S.SQL_FULLTEXT, S.SQL_PROFILE, S.EXECUTIONS, S.LAST_LOAD_TIME, S.PARSING_USER_ID, S.SERVICE FROM SYS.V_$SQL S, SYS.ALL_USERS U WHERE S.PARSING_USER_ID=U.USER_ID AND UPPER(U.USERNAME) … Read more

Dynamic SQL (passing table name as parameter)

Well, firstly you’ve omitted the ‘+’ from your string. This way of doing things is far from ideal, but you can do DECLARE @SQL varchar(250) SELECT @SQL = ‘SELECT * FROM ‘ + QuoteName(@Alias) Exec(@SQL) I’d strongly suggest rethinking how you do this, however. Generating Dynamic SQL often leads to SQL Injection vulnerabilities as well … Read more