Click here to Skip to main content
15,350,151 members
Home / Discussions / Database
   

Database

 
GeneralRe: Help with a SQL query Pin
Stephen Holdorf20-May-15 3:39
MemberStephen Holdorf20-May-15 3:39 
AnswerRe: Help with a SQL query with all inappropriate information removed Pin
Richard Deeming1-May-15 3:39
mveRichard Deeming1-May-15 3:39 
QuestionRe: Test sol Pin
Eddy Vluggen4-May-15 8:20
professionalEddy Vluggen4-May-15 8:20 
QuestionDynamically Add Column Name Pin
MadDashCoder29-Apr-15 3:44
MemberMadDashCoder29-Apr-15 3:44 
AnswerRe: Dynamically Add Column Name Pin
Sascha Lefèvre29-Apr-15 5:11
professionalSascha Lefèvre29-Apr-15 5:11 
GeneralRe: Dynamically Add Column Name Pin
MadDashCoder29-Apr-15 13:43
MemberMadDashCoder29-Apr-15 13:43 
GeneralRe: Dynamically Add Column Name Pin
Corporal Agarn30-Apr-15 1:48
professionalCorporal Agarn30-Apr-15 1:48 
GeneralRe: Dynamically Add Column Name Pin
Richard Deeming30-Apr-15 1:52
mveRichard Deeming30-Apr-15 1:52 
Try this:
SQL
CREATE PROC dbo.GetName
(
    @column        sysname,
    @columnData    nvarchar(50)
)
As
BEGIN
DECLARE @MyQuery nvarchar(max), @RealColumnName nvarchar(130);
    
    SET NOCOUNT ON;
    
    -- Verify that the column name exists in the table:
    SELECT
        @RealColumnName = QuoteName(name)
    FROM
        sys.columns
    WHERE
        object_id = OBJECT_ID('dbo.TableA')
    And
        name = @column
    ;
    
    If @RealColumnName Is Null
    BEGIN
        RAISERROR('Invalid column name: "%s"', 16, 1, @column);
        Return;
    END;
    
    -- Use sp_executesql to pass the parameter to the dynamic query as a parameter:
    -- https://msdn.microsoft.com/en-gb/library/ms188001.aspx
    SET @MyQuery = N'SELECT ' + @RealColumnName + N' FROM dbo.TableA WHERE (' + @RealColumnName + N' Like @columnData + N''%'' Or @columnData Is Null)';
    EXEC sp_executesql @MyQuery, N'@columnData nvarchar(50)', @columnData = @columnData;
END




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Dynamically Add Column Name Pin
Corporal Agarn30-Apr-15 6:38
professionalCorporal Agarn30-Apr-15 6:38 
AnswerRe: Dynamically Add Column Name Pin
Corporal Agarn29-Apr-15 5:37
professionalCorporal Agarn29-Apr-15 5:37 
GeneralRe: Dynamically Add Column Name Pin
MadDashCoder30-Apr-15 6:11
MemberMadDashCoder30-Apr-15 6:11 
QuestionStarting a SSIS 2012 DTSX package without validating it? Pin
Dr Miroslav Stimac29-Apr-15 1:39
professionalDr Miroslav Stimac29-Apr-15 1:39 
AnswerRe: Starting a SSIS 2012 DTSX package without validating it? Pin
Snorri Kristjansson22-May-15 1:39
professionalSnorri Kristjansson22-May-15 1:39 
Questiondatabase Pin
surender singh28-Apr-15 22:01
Membersurender singh28-Apr-15 22:01 
AnswerRe: database Pin
Richard MacCutchan28-Apr-15 23:05
mveRichard MacCutchan28-Apr-15 23:05 
AnswerRe: database Pin
Richard Deeming29-Apr-15 1:55
mveRichard Deeming29-Apr-15 1:55 
QuestionMySQL Server does not show up in Installer Pin
QuickBooksDev28-Apr-15 0:26
MemberQuickBooksDev28-Apr-15 0:26 
AnswerRe: MySQL Server does not show up in Installer Pin
Herman<T>.Instance29-Apr-15 9:57
MemberHerman<T>.Instance29-Apr-15 9:57 
QuestionDoubt in oralce table Pin
Balaji Naidu27-Apr-15 9:39
MemberBalaji Naidu27-Apr-15 9:39 
QuestionRe: Doubt in oralce table Pin
Richard Deeming27-Apr-15 10:21
mveRichard Deeming27-Apr-15 10:21 
AnswerRe: Doubt in oralce table Pin
Balaji Naidu27-Apr-15 10:26
MemberBalaji Naidu27-Apr-15 10:26 
AnswerRe: Doubt in oralce table Pin
Sascha Lefèvre27-Apr-15 15:15
professionalSascha Lefèvre27-Apr-15 15:15 
QuestionOrder By Pin
jkirkerx26-Apr-15 11:57
professionaljkirkerx26-Apr-15 11:57 
AnswerRe: Order By Pin
David Mujica27-Apr-15 3:37
MemberDavid Mujica27-Apr-15 3:37 
GeneralRe: Order By Pin
jkirkerx27-Apr-15 6:40
professionaljkirkerx27-Apr-15 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.