Вопрос по sql – Могу ли я передать имя столбца в качестве входного параметра в хранимой процедуре SQL

28
create procedure sp_First
@columnname varchar
AS
begin
select @columnname from Table_1
end 
exec sp_First 'sname'

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded ctbrown

Ваш Ответ

8   ответов
10

DECLARE @sql nvarchar(max) = 'SELECT ' + @columnname + ' FROM Table_1';
exec sp_executesql @sql, N''
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

   Create PROCEDURE USP_S_NameAvilability
     (@Value VARCHAR(50)=null,
      @TableName VARCHAR(50)=null,
      @ColumnName VARCHAR(50)=null)
        AS
        BEGIN
        DECLARE @cmd AS NVARCHAR(max)
        SET @Value = ''''[email protected]+ ''''
        SET @cmd = N'SELECT * FROM ' + @TableName + ' WHERE ' +  @ColumnName + ' = ' + @Value
        EXEC(@cmd)
      END

Error: User Rate Limit Exceeded

9

Error: User Rate Limit Exceeded

create procedure sp_First @columnname varchar 
AS 
begin 
    declare @sql nvarchar(4000);
    set @sql='select ['[email protected]+'] from Table_1';
    exec sp_executesql @sql
end 
go

exec sp_First 'sname'
go
2

Error: User Rate Limit Exceeded

Select @Columnname From Table

Error: User Rate Limit ExceededEXEC (@SQL)

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

-5

Error: User Rate Limit Exceeded

Create Procedure Test
(
    @Table VARCHAR(500),
    @Column VARCHAR(100),
    @Value  VARCHAR(300)
)
AS
BEGIN

DECLARE @sql nvarchar(1000)

SET @sql = 'SELECT * FROM ' + @Table + ' WHERE ' + @Column + ' = ' + @Value

--SELECT @sql
exec (@sql)

END

-----execution----

/** Exec Test Products,IsDeposit,1 **/
Error: User Rate Limit Exceeded
6

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

Error: User Rate Limit Exceeded

            CREATE PROCEDURE [dbo].[DASH_getTP_under_TP]
    (
    @fromColumnName varchar(10) ,
    @toColumnName varchar(10) , 
    @ID varchar(10)
    )
    as
    begin

    -- this is the column required for where clause 
    declare @colname varchar(50)
    set @colname=case @fromUserType
        when 'XX' then 'XX'
        when 'YY' then 'YY'
        end
        select SelectedColumnId  from (
       select 
            case @toColumnName 
            when 'XX' then tablename.XX
            when 'YY' then tablename.YY
            end as SelectedColumnId,
        From tablename
        where 
        (case @fromUserType 
            when 'XX' then XX
            when 'YY' then YY
        end)= ISNULL(@ID , @colname) 
    ) as tbl1 group by SelectedColumnId 

    end
52

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

SET @sql = 'SELECT ' + @columnName + ' FROM yourTable'
sp_executesql @sql

Error: User Rate Limit ExceededalwaysError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

SELECT
  CASE @columnName
    WHEN 'Col1' THEN Col1
    WHEN 'Col2' THEN Col2
                ELSE NULL
  END as selectedColumn
FROM
  yourTable

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
+1, This is a bit more long winded, but a whole lot more secure.

Похожие вопросы