In my today post, we will be discussing how to declare a single or multiple variables with different approaches by using SQL Statements. I will be using MS SQL Server to write SQL Statements to achieve the task.
Lets get logged in to your SQL Server Database Engine using Microsoft SQL Server Management Studio.
Create a New Query, I know that you are most familiar with how to open a new Query Window in MS SQL Server.
Here i am using a Sample Database with a Name sampledatabase I will be using the same database for writing the examples with my Blog posts.
I have Created a Table to add some Records so that we can use them in our on the go post.
create table MembersList
(
id bigint not null primary key identity(1,1)
,Name nvarchar(100)
,ContactNo nvarchar(50)
,EmailID nvarchar(100)
,Age int
,FK_MemberType bigint
,ListedDateTime DateTime
,AddressLocation nvarchar(1000)
)
Now i will be adding some Records in the Table.
The Insert Query Helps me to add the details in the Table.
insert into MembersList(Name,ContactNo ,EmailID,Age,FK_MemberType,ListedDateTime,AddressLocation)
select 'Liam','+44 -0125365422','Liam@Email.com',65,1,GetDate(),'Location Address 1' union
select 'Noah','+44 -0125365423','Noah@Email.com',25,2,GetDate(),'Location Address 2' union
select 'Oliver','+44 -0125365424','Oliver@Email.com',45,1,GetDate(),'Location Address 3' union
select 'Elijah','+44 -0125365425','Elijah@Email.com',23,2,GetDate(),'Location Address 4' union
select 'William','+44 -0125365426','William@Email.com',45,3,GetDate(),'Location Address 5' union
select 'James','+44 -0125365427','James@Email.com',50,1,GetDate(),'Location Address 6' union
select 'Benjamin','+44 -0125365428','Benjamin@Email.com',21,1,GetDate(),'Location Address 7' union
select 'Lucas','+44 -0125365429','Lucas@Email.com',20,1,GetDate(),'Location Address 8'
select 'declare @' + COLUMN_NAME + ' as ' + DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='MembersList'
declare @id as bigint
declare @Name as nvarchar
declare @ContactNo as nvarchar
declare @EmailID as nvarchar
declare @Age as int
declare @FK_MemberType as bigint
declare @ListedDateTime as datetime
declare @AddressLocation as nvarchar
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='MembersList'
- TABLE_CATALOG
- TABLE_SCHEMA
- TABLE_NAME
- COLUMN_NAME
- ORDINAL_POSITION
- COLUMN_DEFAULT
- IS_NULLABLE
- DATA_TYPE
- CHARACTER_MAXIMUM_LENGTH
- CHARACTER_OCTET_LENGTH
- NUMERIC_PRECISION
- NUMERIC_PRECISION_RADIX
- NUMERIC_SCALE
- DATETIME_PRECISION
- CHARACTER_SET_CATALOG
- CHARACTER_SET_SCHEMA
- CHARACTER_SET_NAME
- COLLATION_CATALOG
- COLLATION_SCHEMA
- COLLATION_NAME
- DOMAIN_CATALOG
- DOMAIN_SCHEMA
- DOMAIN_NAME
0 Comments
Post a Comment