Tweet
When we import, export or synchronise tables, it's a hard job if we have many records.
In the past, I've found some difficulties when I need to compare two tables in SQL Server.
The tables contained different records and I needed to consolidate them in the first table.
The first thing I wanted to do was to actually see which were the differences: I needed a list of records in order to understand the situation.
I've found a good solution for that, and let me say, a quite unexpected solution indeed.
Showing posts with label sql query. Show all posts
Showing posts with label sql query. Show all posts
Tuesday, 5 March 2013
Thursday, 7 February 2013
SQL: AND vs OR (operators)
Tweet
In SQL Server we can use different operators in our queries: AND, OR, LIKE, BETWEEN and so on. In this short post we are going to see AND and OR because I've noticed that some could get confused by the results obtained when using them.
So, let's clear the situation a bit, because we really need to know what we are doing!
In SQL Server we can use different operators in our queries: AND, OR, LIKE, BETWEEN and so on. In this short post we are going to see AND and OR because I've noticed that some could get confused by the results obtained when using them.
So, let's clear the situation a bit, because we really need to know what we are doing!
Tuesday, 29 January 2013
SQL: which columns should have an index
Tweet
In SQL tables might have primary keys and indexes. The primary key identifies each record. The key must be unique, it can't be NULL and each table must have just one key.
Indexes are something different: they are used to speed up the queries on a specific table. That means we absolutely need indexes on large tables which we often query.
In SQL tables might have primary keys and indexes. The primary key identifies each record. The key must be unique, it can't be NULL and each table must have just one key.
Indexes are something different: they are used to speed up the queries on a specific table. That means we absolutely need indexes on large tables which we often query.
Thursday, 24 January 2013
SQL: subqueries
Tweet
In this short post, I would like to introduce the concept of subqueries in SQL.
A subquery is a query statement inside another query statement. As you already know the order in which a statement is processed is quite important. That is a basic rule like in maths: writing something like 2+2*5 is different than writing (2+2)*5. The first operation gives 12, the second 20.
In SQL the principle is identical.
In this short post, I would like to introduce the concept of subqueries in SQL.
A subquery is a query statement inside another query statement. As you already know the order in which a statement is processed is quite important. That is a basic rule like in maths: writing something like 2+2*5 is different than writing (2+2)*5. The first operation gives 12, the second 20.
In SQL the principle is identical.
Tuesday, 11 December 2012
SQL: the power of left, right and IN
Tweet
As the title mentions, in this short article I will show you how to benefit from the use of the left and right SQL functions together with the IN operator.
Before doing so, we need to understand what are the two functions and what the operator is doing.
Please follow me and see what we can do!
As the title mentions, in this short article I will show you how to benefit from the use of the left and right SQL functions together with the IN operator.
Before doing so, we need to understand what are the two functions and what the operator is doing.
Please follow me and see what we can do!
Monday, 4 June 2012
SQL Server: useful query for DBAs
Tweet
If you are a Database Administrator and you work with MS SQL Server, there are some useful queries you could use to check how things are going with your databases. Being always aware of what's going on is very important, and in some cases a good control panel with appropriate queries can be developed just to quickly display the databases status.
We are going to explore some interesting administration queries. Just select those that you might be interested in and add them to your personal control panel.
If you are a Database Administrator and you work with MS SQL Server, there are some useful queries you could use to check how things are going with your databases. Being always aware of what's going on is very important, and in some cases a good control panel with appropriate queries can be developed just to quickly display the databases status.
We are going to explore some interesting administration queries. Just select those that you might be interested in and add them to your personal control panel.
Friday, 4 May 2012
ASP: avoid SQL injections
Tweet
If you work a lot with ASP and SQL, you might already know about SQL injections.
It's quite clear that every time we offer a visitor the possibility of filling a form (whatever its purpose may be), there's a security risk for our database. Basically, an attacker can insert some code in a form element, which will serve as a breach, allowing access to data stored in the database.
How that's done is not the main topic of this short post, however we should be aware of the fact that those threats are often used to update, delete and insert data, or in worst cases, they are used in order to gain access to reserved areas of a web site.
In this article, we are going to create a small VBScript function to avoid SQL injections.
If you work a lot with ASP and SQL, you might already know about SQL injections.
It's quite clear that every time we offer a visitor the possibility of filling a form (whatever its purpose may be), there's a security risk for our database. Basically, an attacker can insert some code in a form element, which will serve as a breach, allowing access to data stored in the database.
How that's done is not the main topic of this short post, however we should be aware of the fact that those threats are often used to update, delete and insert data, or in worst cases, they are used in order to gain access to reserved areas of a web site.
In this article, we are going to create a small VBScript function to avoid SQL injections.
Wednesday, 4 April 2012
SQL: wildcards
Tweet
While we all know about the "%" wildcard, we might not know that there are other interesting ways of using special characters when building queries.
I suppose you are aware of what a wildcard is. Anyway, if you're new to SQL, wildcards are special characters used in queries and they can be used to substitute one or more characters while retrieving data from a database.
We must remember that we can use wildcards only with the "LIKE" operator.
Before going into the available wildcards, let me show you a basic example:
Now, let's see which are the available wildcards.
While we all know about the "%" wildcard, we might not know that there are other interesting ways of using special characters when building queries.
I suppose you are aware of what a wildcard is. Anyway, if you're new to SQL, wildcards are special characters used in queries and they can be used to substitute one or more characters while retrieving data from a database.
We must remember that we can use wildcards only with the "LIKE" operator.
Before going into the available wildcards, let me show you a basic example:
SELECT productCode, description, price
FROM prodTable
WHERE description LIKE 'p%'
The above query will retrieve all the products that have a description starting with "p" from the prodTable table.Now, let's see which are the available wildcards.
Monday, 12 March 2012
SQL & ASP: custom queries for the user (a theoretical post)
Tweet
This post was conceived as a theoretical experiment on dynamic queries. The original idea was to let the user create custom SQL queries against a database.
The whole thing is not so crazy, and it kept forming in my mind while I was thinking about it.
Imagine a situation where a user can select a table from the list of tables in a SQL Server database. That is not a problem; infact we can retrieve table names with:
This post was conceived as a theoretical experiment on dynamic queries. The original idea was to let the user create custom SQL queries against a database.
The whole thing is not so crazy, and it kept forming in my mind while I was thinking about it.
Imagine a situation where a user can select a table from the list of tables in a SQL Server database. That is not a problem; infact we can retrieve table names with:
SELECT name
FROM sys.Tables
Wednesday, 15 February 2012
SQL: the alter table statement
Tweet
In SQL we usually create tables, pre-deciding the columns and their characteristics, like default values and data types. It is possible to insert new columns and eventually alter them afterwards, using the alter table statement.
In the following post we are going to see what we can do with it.
With alter table, we can add, change the data type or delete a column inside a table.
In SQL we usually create tables, pre-deciding the columns and their characteristics, like default values and data types. It is possible to insert new columns and eventually alter them afterwards, using the alter table statement.
In the following post we are going to see what we can do with it.
With alter table, we can add, change the data type or delete a column inside a table.
Friday, 20 January 2012
SQL: the IN operator
Tweet
The IN operator can be very useful when we need to perform SQL queries where we need to filter the results with more than one possible value.
Just to understand the power of the IN operator, we need to go back a few steps and see how we usually filter data from a table with the OR operator.
The IN operator can be very useful when we need to perform SQL queries where we need to filter the results with more than one possible value.
Just to understand the power of the IN operator, we need to go back a few steps and see how we usually filter data from a table with the OR operator.
Friday, 16 December 2011
ASP: how to create a reusable function to query databases
Tweet
Today we are going to create something probably out of the ordinary. We will create two reusable functions (with parameters) in order to query a database.
The first function will open a recordset and execute a query. It will accept 3 parameters: recordset name, connection and query string.
The second function will close the recordset. It will accept 1 parameter: recordset name.
In order to use the parametrised functions, we will benefit from the Execute() VBScript function.
So, before building the two functions, let's try to understand Execute().
Today we are going to create something probably out of the ordinary. We will create two reusable functions (with parameters) in order to query a database.
The first function will open a recordset and execute a query. It will accept 3 parameters: recordset name, connection and query string.
The second function will close the recordset. It will accept 1 parameter: recordset name.
In order to use the parametrised functions, we will benefit from the Execute() VBScript function.
So, before building the two functions, let's try to understand Execute().
Friday, 11 November 2011
SQL: how to update two (or more) tables at the same time
Tweet
******IMPORTANT NOTE******
Only now after a few years of comments, I understand the big confusion I generated, and I understand it's been my fault and for that I do apologize.
The query works but NOT in MSSMS: the query works in ASP or in a Ms Access environment with linked tables to MS SQL. I hope this clarify a bit...
Sometimes, when searching for an answer, we end up making things too much complicated, while easy solutions are just round the corner. This is the case of a simple task like updating two related tables with just one SQL query.
Suppose we have two related tables. The first contains user names, and the second email addresses related to the first table names.
First table ("names")
The second table ("addresses")
How do we change the name and the street of the first record (with id equal to 1)?
With one simple query.
******IMPORTANT NOTE******
Only now after a few years of comments, I understand the big confusion I generated, and I understand it's been my fault and for that I do apologize.
The query works but NOT in MSSMS: the query works in ASP or in a Ms Access environment with linked tables to MS SQL. I hope this clarify a bit...
Sometimes, when searching for an answer, we end up making things too much complicated, while easy solutions are just round the corner. This is the case of a simple task like updating two related tables with just one SQL query.
Suppose we have two related tables. The first contains user names, and the second email addresses related to the first table names.
First table ("names")
| ID | name |
| 1 | John |
| 2 | James |
The second table ("addresses")
| ID | address |
| 1 | First Street |
| 2 | Second Street |
How do we change the name and the street of the first record (with id equal to 1)?
With one simple query.
Wednesday, 2 November 2011
ASP: create an automatic email alert
Tweet
Recently, I had to create an automatic alert for a company intranet. The final user wanted to receive an e-mail - and just one email - a month prior to an expiration date. Basically I had a list of names and a related date which represent an issue date for a document. This document expires after one year and the final user needed to receive an email alert a month before the expiration date.
It looks like an easy task, doesn't it? In the following post, we are going to see the logic behind it and how to build the alert system.
As said above, we need a table (called 'table') in our database with a date field. The date is representing our expiration date, and we call it 'exp_date'.
Then we need another field, that we can call 'email_notification'.
There will surely be other fields in our table, however, for the purpose of the following exercise, we don't really care. Let's say we have just another field, called 'name' (aside for the usual id field).
Thus, our table structure is the following:
id - int (unique)
name - varchar(255)
exp_date - smalldatetime
email_notification - numeric(18,0) - default value = 0
That is all.
Recently, I had to create an automatic alert for a company intranet. The final user wanted to receive an e-mail - and just one email - a month prior to an expiration date. Basically I had a list of names and a related date which represent an issue date for a document. This document expires after one year and the final user needed to receive an email alert a month before the expiration date.
It looks like an easy task, doesn't it? In the following post, we are going to see the logic behind it and how to build the alert system.
Some assumptions
As usual we need to consider some things before going into the theory behind it all.As said above, we need a table (called 'table') in our database with a date field. The date is representing our expiration date, and we call it 'exp_date'.
Then we need another field, that we can call 'email_notification'.
There will surely be other fields in our table, however, for the purpose of the following exercise, we don't really care. Let's say we have just another field, called 'name' (aside for the usual id field).
Thus, our table structure is the following:
id - int (unique)
name - varchar(255)
exp_date - smalldatetime
email_notification - numeric(18,0) - default value = 0
That is all.
Wednesday, 19 October 2011
ASP: conditional update or insert record
Tweet
There are situations when we need to insert or update a record in a table according to its presence. The condition is: if the record is already in the table, then update it, otherwise insert it.
Here we will see how to do it.
I will show you an example, that will apply to a specific situation, however, after understanding the logic, you can think of something related to your personal goal.
In the following example, we have a table (table1) with 3 columns: id, field1 and field2. We have a form submitting data. Then there's another table (table2), linked to the first table through the id field. The columns of this second table are: id and field3.
The first table (table1) might have records linked to the second table (table2), while table2 already contains records. That is a classic situation in relational databases. The point here is that we know that a unique record is present in table2, and we want to insert a new unique record in table1 if it's not already present, otherwise we need to update it.
I hope that the above explanation is clear enough.
There are situations when we need to insert or update a record in a table according to its presence. The condition is: if the record is already in the table, then update it, otherwise insert it.
Here we will see how to do it.
Some assumptions
To achive our goal, the first task is to determine which is the condition upon which we decide to update or insert the record.I will show you an example, that will apply to a specific situation, however, after understanding the logic, you can think of something related to your personal goal.
In the following example, we have a table (table1) with 3 columns: id, field1 and field2. We have a form submitting data. Then there's another table (table2), linked to the first table through the id field. The columns of this second table are: id and field3.
The first table (table1) might have records linked to the second table (table2), while table2 already contains records. That is a classic situation in relational databases. The point here is that we know that a unique record is present in table2, and we want to insert a new unique record in table1 if it's not already present, otherwise we need to update it.
I hope that the above explanation is clear enough.
Friday, 17 June 2011
SQL Server: use the NewId() function to create unique identifiers
Tweet
A few months ago, I have published an article mentioning the NewId() SQL function. In that post, I was explaining how to create dynamic keywords and description meta tags. However the NewId() function can be used in different situations and it becomes very handy, when we need unique identifiers.
Let's see how it works.
A few months ago, I have published an article mentioning the NewId() SQL function. In that post, I was explaining how to create dynamic keywords and description meta tags. However the NewId() function can be used in different situations and it becomes very handy, when we need unique identifiers.
Let's see how it works.
The basics
The NewId() function returns a unique identifier; that means a unique set of numbers and letters. If we use the function in a query in SQL Server Management Studio like:
print newid()
the result will be a unique identifier like:
5C6C9372-49F9-4A5C-BCFA-DA6CDB525CAB
Interestingly enough, the above result is unique and can be used in various way. For example, we can use the function to generate personal identification numbers (PINs), login passwords, ids, serial numbers or product keys, simple primary keys etc. At the same time we can use the function to create random sorting of data.Wednesday, 2 March 2011
SQL Server: stored procedure with code fetched from database
Tweet
I know this is crazy, but I've been thinking about it for a while. In the past I've talked about stored procedures with clauses taken from another table. Ok, I understand that this is difficult to explain. Let me try.
Let's say I have a table that contains all the company departments. I actually use this table to create parameters for a where-clause in a stored procedure. That is done because I need to change departments without having to change the stored procedure. When a new department is inserted into the departments table, the stored procedure works immediately, without the need to change it.
Said that, please follow me. Let's say I have 3 stored procedures that perform queries on different tables with appropriate joins. Part of the code of these 3 stored procedures is exactly the same. When I have to change that part of the code, I have to change it 3 times.
Would it be crazy if I put that part of the code in a table, fetch it, and use it in the stored procedure?
In my SQL Server database, I have more than 3 stored procedures in that situation. I understand that it is absolutely possible - in coding terms - to do what I said. What really worry me is: would it pose some performance issue?
I really would like if you share your thoughts on this.
I know this is crazy, but I've been thinking about it for a while. In the past I've talked about stored procedures with clauses taken from another table. Ok, I understand that this is difficult to explain. Let me try.
Let's say I have a table that contains all the company departments. I actually use this table to create parameters for a where-clause in a stored procedure. That is done because I need to change departments without having to change the stored procedure. When a new department is inserted into the departments table, the stored procedure works immediately, without the need to change it.
Said that, please follow me. Let's say I have 3 stored procedures that perform queries on different tables with appropriate joins. Part of the code of these 3 stored procedures is exactly the same. When I have to change that part of the code, I have to change it 3 times.
Would it be crazy if I put that part of the code in a table, fetch it, and use it in the stored procedure?
In my SQL Server database, I have more than 3 stored procedures in that situation. I understand that it is absolutely possible - in coding terms - to do what I said. What really worry me is: would it pose some performance issue?
I really would like if you share your thoughts on this.
loading..
Monday, 14 February 2011
SQL Server: apostrophes in queries
Tweet
Apostrophes ( ' ) in queries might be a big problem. In fact, we usually build our queries without thinking about it, maybe because it's quite unusual to have data containing such strange characters. That leads to difficulties in isolating the cause of an error while executing a query statement, especially when the query worked for a year and then - suddenly - starts giving problems. It is quite common not to think about new data recently entered or updated, and usually we concentrate on the query statement and thus - let me say - waste a lot of time/money.
Apostrophes ( ' ) in queries might be a big problem. In fact, we usually build our queries without thinking about it, maybe because it's quite unusual to have data containing such strange characters. That leads to difficulties in isolating the cause of an error while executing a query statement, especially when the query worked for a year and then - suddenly - starts giving problems. It is quite common not to think about new data recently entered or updated, and usually we concentrate on the query statement and thus - let me say - waste a lot of time/money.
Wednesday, 2 February 2011
SQL Server: Stored Procedures and variables problem
Tweet
In my last post I published a step-by-step guide to create Stored Procedures in SQL Server using SQL Server Management Studio.
There are some things I would like to add, because in my experience I sometimes got stuck with errors that - when solved - were so easy to pinpoint, while little information can be found in tech forums or generally in tech sites. Specifically there is a common problem with the @query variable and its length.
In my last post I published a step-by-step guide to create Stored Procedures in SQL Server using SQL Server Management Studio.
There are some things I would like to add, because in my experience I sometimes got stuck with errors that - when solved - were so easy to pinpoint, while little information can be found in tech forums or generally in tech sites. Specifically there is a common problem with the @query variable and its length.
Monday, 31 January 2011
SQL Server: How to create Stored Procedures
Tweet
"How do I create Stored Procedures?" ... ... ok, I know, this is very basic, but do not feel ashamed if you don't know the answer. When I started playing with SQL Server, Stored Procedures seemed to me a secret and foreign land where everything is difficult and hard to understand. Well, I must say, it is not. So get ready, put on your armor and let's go to slay the dragon (a.k.a. the Stored Procedure)!
"How do I create Stored Procedures?" ... ... ok, I know, this is very basic, but do not feel ashamed if you don't know the answer. When I started playing with SQL Server, Stored Procedures seemed to me a secret and foreign land where everything is difficult and hard to understand. Well, I must say, it is not. So get ready, put on your armor and let's go to slay the dragon (a.k.a. the Stored Procedure)!
Subscribe to:
Posts (Atom)
