Dot Net Learning Zone Blog

A blog about to learn Dot Net

Archive for the ‘SQL Question’ Category

How to get value using SP_EXECUTESQL

Posted by Rajesh on March 10, 2009

DECLARE @Name varchar(50)
DECLARE @Query nvarchar(200)
DECLARE @ParmDefinition NVARCHAR(500)
SET @Query =’Select  @Name=CompanyName  from CompanyDetail’
SET @ParmDefinition = N’@Name varchar(50) OUTPUT’
EXEC SP_EXECUTESQL @Query,@ParmDefinition,@Name=@Name OUTPUT
SELECT @Name

Posted in SQL, SQL Question | Leave a Comment »

Find duplicate row in table in SQL

Posted by Rajesh on May 27, 2008

1. Create Table

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[tbl_Test](

[Id] [int] NULL,

[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

2. Insert Record

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (1,‘a’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (2,‘b’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (3,‘c’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (1,‘a’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (3,‘c’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (4,‘m’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (5,‘p’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (4,‘m’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (6,‘d’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (7,‘y’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (8,‘o’)

INSERT INTO [Test].[dbo].[tbl_Test]([Id] ,[Name]) VALUES (9,‘f’)

3. Find the duplicate record on the basis on Name field

1)

SELECT DISTINCT * FROM dbo.tbl_Test A

WHERE 1<(SELECT COUNT(*) FROM tbl_Test B

WHERE A.nAME=B.NAME)ORDER BY A.NAME

2)

SELECT ID, NAME FROM dbo.tbl_Test

GROUP BY NAME,ID

HAVING COUNT(*)>1

Posted in SQL Question | Leave a Comment »

FAQ in SQL 2000/2005

Posted by Rajesh on August 11, 2007

Posted in SQL Question | 1 Comment »

Interview question in SQL

Posted by Rajesh on August 3, 2007

Which of the following data types has the highest data type precedence?

A. INT
B. VARCHAR
C. DATETIME
D. FLOAT
E. NUMERIC
F. MONEY

ans :- Here is the order of data type precedence of the given options from highest to lowest: DATETIME, FLOAT, NUMERIC, MONEY, INT and VARCHAR.

2) What’s the maximum number of columns can you include in a SELECT statement?

A. 512
B. 1,024
C. 2,048
D. 4,096

ANS
Although the maximum number of columns a table can have is 1,024, the maximum number of columns that can be specified in a SELECT statement is 4,096.

3) Which of the following is NOT a valid description of the public role?

A. The public role captures all default permissions for users in a database.
B. The public role cannot be dropped.
C. The public role is contained in every database, including msdb, tempdb, model, and all user databases except in the master database for security purposes.
D. The public role cannot have users, groups, or roles assigned to it.

ans:-
All of these statements are valid description of the public role except for the third option because the public role is also contained in the master database.
4)
What will be the output of the following statement?

SELECT CHARINDEX(‘is’, ‘Missississippi’, 0)

A. 0
B. 2
C. NULL
D. Statement will generate an error. The CHARDINDEX function only accepts 2 parameters.

Your Answer : D

Correct Answer : B

Explanation:
The CHARINDEX function returns the starting position of the specified expression in a character string. The third parameter of the CHARINDEX functions is the character position to start searching for the first parameter in the second parameter. If the starting location is 0, the search starts at the beginView text definitions are stored in which system table?

A. sysobjects
B. syscolumns
C. syscomments
D. sysviews

Your Answer : A

Correct Answer : C

Explanation:
The syscomments system table contains entries for each view, rule, default, trigger, CHECK constraint, DEFAULT constraint and stored procedure. The text column contains the original SQL definition statements.
ning of the second parameter.

5)
View text definitions are stored in which system table?

A. sysobjects
B. syscolumns
C. syscomments
D. sysviews

Your Answer : A

Correct Answer : C

Explanation:
The syscomments system table contains entries for each view, rule, default, trigger, CHECK constraint, DEFAULT constraint and stored procedure. The text column contains the original SQL definition statements.

6)
What will be the output of the following statement?

SELECT CHARINDEX(‘is’, ‘Missississippi’, 3)

A. 2
B. 5
C. 8
D. 9
E. -1
F. 0

Your Answer : B

Correct Answer : B

Explanation:
The CHARINDEX function returns the starting position of the specified expression in a character string. The third parameter of the CHARINDEX functions is the character position to start searching for the first parameter in the second parameter.

7
What will be the result of the following statement?

SELECT CAST(-1 AS DATETIME)

A. 1900-01-01 00:00:00.000
B. 1899-01-01 00:00:00.000
C. 1899-12-31 00:00:00.000
D. 1752-01-01 00:00:00.000
E. 1752-12-31 00:00:00.000
F. The statement will generate an error. Only positive integer values can be converted to a DATETIME data type.

Your Answer : F

Correct Answer : C

Explanation:
A DATETIME data type is stored internally in SQL Server as two 4-byte integers. The first 4 bytes store the number of days before or after the base date of January 1, 1900. Given this, a value of 0 corresponds to January 1, 1900 and a value of -1 corresponds to 1 day before the base date, December 31, 1899.

8) What will be the result of the following statement?

SELECT CAST(-1 AS DATETIME)

A. 1900-01-01 00:00:00.000
B. 1899-01-01 00:00:00.000
C. 1899-12-31 00:00:00.000
D. 1752-01-01 00:00:00.000
E. 1752-12-31 00:00:00.000
F. The statement will generate an error. Only positive integer values can be converted to a DATETIME data type.

Your Answer : F

Correct Answer : C

Explanation:
A DATETIME data type is stored internally in SQL Server as two 4-byte integers. The first 4 bytes store the number of days before or after the base date of January 1, 1900. Given this, a value of 0 corresponds to January 1, 1900 and a value of -1 corresponds to 1 day before the base date, December 31, 1899.

Posted in SQL Question | 2 Comments »