SQL Server: How can I select everything from a table with a prefix? -
i have following code in long stored procedure, p equals products table:
select p.*, etc1, etc2
which give me "productid" , on.
i select prefix such as:
select p.* p_*, etc1, etc2
which give me "p_productid" , on.
is possible do?
not unless use dynamic sql. uncommon require such thing though, sure need it?
working example
create table products (productid int, price money, description varchar(10)); insert products select 1, 12.3, 'apples' insert products select 2, 2.4, 'bananas' create table orderdetails (orderid int, productid int, qty int) insert orderdetails select 11,1, 2 insert orderdetails select 11,2, 4 declare @sql nvarchar(max) select @sql = coalesce(@sql+',','') + 'p.' + quotename(column_name) + ' ' + quotename('p_' + column_name) information_schema.columns table_name = 'products' order ordinal_position set @sql = ' select ' + @sql + ', o.orderid, o.qty products p inner join orderdetails o on p.productid = o.productid ' --print @sql :: uncomment if need see exec (@sql)
output:
p_productid p_price p_description orderid qty ----------- --------------------- ------------- ----------- ----------- 1 12.30 apples 11 2 2 2.40 bananas 11 4
Comments
Post a Comment