Database does not exist. Make sure that the name is entered correctly.

Database does not exist. Make sure that the name is entered correctly.

USE [InvoiceDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EmployeeDemographics](
	[FirstName] [nvarchar](50) NULL,
	[LastName] [nvarchar](50) NULL,
	[Age] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[EmployeeDemographics] ([FirstName], [LastName], [Age]) VALUES (N'TEST', N'NAME', 20)
GO
INSERT [dbo].[EmployeeDemographics] ([FirstName], [LastName], [Age]) VALUES (N'NEW', N'NEW', 30)
GO
INSERT [dbo].[EmployeeDemographics] ([FirstName], [LastName], [Age]) VALUES (N'TESTONE', N'ONE', 40)
GO
INSERT [dbo].[EmployeeDemographics] ([FirstName], [LastName], [Age]) VALUES (N'TEST', N'test', NULL)
GO
DECLARE @sql_command NVARCHAR(MAX);
SELECT @sql_command = 'SELECT TOP 10 * FROM  InvoiceDB.dbo.EmployeeDemographics';
EXEC (@sql_command);
DECLARE @sql_command NVARCHAR(MAX);
SELECT @sql_command = 'SELECT TOP 10 * FROM InvoiceDB.dbo.EmployeeDemographics';
EXEC @sql_command;

Remember to always put parentheses around the @sql_command string.

The dynamic SQL command string is treated by SQL Server as a stored procedure
when parentheses are not included.

Leave a Comment