Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following select
that tests if specific schema with specific table name has specific column

SQL
Declare @ColumnName nvarchar(255)
Declare @ColumnType nvarchar(255)
Declare @ColumnLength nvarchar(255)
Declare @DataBaseName nvarchar(255)

SELECT @ColumnName=COLUMN_NAME,@ColumnType=DATA_TYPE,@ColumnLength=CHARACTER_MAXIMUM_LENGTH
          FROM   INFORMATION_SCHEMA.COLUMNS
          WHERE  TABLE_NAME =  @TableName
          and COLUMN_NAME = @ColumnName  and TABLE_SCHEMA=@DataBaseName


My Problem is that the variables must be quoted in select statement and if i put the statement in a string and then executes the string as a solution @ColumnName,@ColumnType,@ColumnLength will not have value unless i put them as out parameters and i tried this way recently but it didnt work
is there any way
to handle that cause this problem always faces me and put me in a state of hopless


Sorry i didnt discribe in a good way i mean by quoted variables
the variables in where clause
i will write the query agian
Collapse | Copy Code

Declare @ColumnName nvarchar(255)
Declare @ColumnType nvarchar(255)
Declare @ColumnLength nvarchar(255)
Declare @DataBaseName nvarchar(255)
Declare @x nvarchar(255)

SELECT @x=COLUMN_NAME,@ColumnType=DATA_TYPE,@ColumnLength=CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
and COLUMN_NAME = @ColumnName and TABLE_SCHEMA=@DataBaseName

the variables @TableName ,@ColumnName,@DataBaseName must be quoted
Suppose i must write the following query

SELECT @x=COLUMN_NAME,@ColumnType=DATA_TYPE,@ColumnLength=CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableA'
and COLUMN_NAME = 'ColumnA' and TABLE_SCHEMA='DataBaseA'

so my question is how can i quote the variables in where statement without writing query statement in string which leads me to put the variables in select statement as out variables
i mean i need the variables in where string to be parsed with double quotation
Posted
Updated 3-Feb-15 21:12pm
v3
Comments
_Asif_ 3-Feb-15 7:14am    
Can you please explain a bit more?
Simon_Whale 3-Feb-15 7:38am    
where are you trying to run this from? SQL or an application being built in a specific language?

Either use...
SQL
Declare @ColumnLength int
to match up the correct type
Or...
,@ColumnLength=CAST(CHARACTER_MAXIMUM_LENGTH AS NVARCHAR(255))
to convert the returned int to the nvarchar you want to store it in.

You don't need the quotes.
 
Share this answer
 
sorry i didnt discribe in a good way i mean by quoted variables
the variables in where clause
i will write the query agian
SQL
Declare @ColumnName nvarchar(255)
Declare @ColumnType nvarchar(255)
Declare @ColumnLength nvarchar(255)
Declare @DataBaseName nvarchar(255)
Declare @x nvarchar(255)

SELECT @x=COLUMN_NAME,@ColumnType=DATA_TYPE,@ColumnLength=CHARACTER_MAXIMUM_LENGTH
          FROM   INFORMATION_SCHEMA.COLUMNS
          WHERE  TABLE_NAME =  @TableName
          and COLUMN_NAME = @ColumnName  and TABLE_SCHEMA=@DataBaseName

the variables @TableName ,@ColumnName,@DataBaseName must be quoted
Suppose i must write the following query

SELECT @x=COLUMN_NAME,@ColumnType=DATA_TYPE,@ColumnLength=CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableA'
and COLUMN_NAME = 'ColumnA' and TABLE_SCHEMA='DataBaseA'

so my question is how can i quote the variables in where statement without writing query statement in string which leads me to put the variables in select statement as out variables
i mean i need the variables in where string to be parsed with double quotation
 
Share this answer
 
Comments
Deepu S Nair 4-Feb-15 2:20am    
Don't post your question as solution.Use 'Improve Question' link to edit your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900