dynamic-sql

Drop all tables whose names begin with a certain string

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 07:01:52
问题 How can I drop all tables whose names begin with a given string? I think this can be done with some dynamic SQL and the INFORMATION_SCHEMA tables. 回答1: You may need to modify the query to include the owner if there's more than one in the database. DECLARE @cmd varchar(4000) DECLARE cmds CURSOR FOR SELECT 'drop table [' + Table_Name + ']' FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'prefix%' OPEN cmds WHILE 1 = 1 BEGIN FETCH cmds INTO @cmd IF @@fetch_status != 0 BREAK EXEC(@cmd) END

Drop all tables whose names begin with a certain string

淺唱寂寞╮ 提交于 2019-12-17 07:01:04
问题 How can I drop all tables whose names begin with a given string? I think this can be done with some dynamic SQL and the INFORMATION_SCHEMA tables. 回答1: You may need to modify the query to include the owner if there's more than one in the database. DECLARE @cmd varchar(4000) DECLARE cmds CURSOR FOR SELECT 'drop table [' + Table_Name + ']' FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'prefix%' OPEN cmds WHILE 1 = 1 BEGIN FETCH cmds INTO @cmd IF @@fetch_status != 0 BREAK EXEC(@cmd) END

PostgreSQL convert columns to rows? Transpose?

二次信任 提交于 2019-12-17 06:37:17
问题 I have a PostgreSQL function (or table) which gives me the following output: Sl.no username Designation salary etc.. 1 A XYZ 10000 ... 2 B RTS 50000 ... 3 C QWE 20000 ... 4 D HGD 34343 ... Now I want the Output as below: Sl.no 1 2 3 4 ... Username A B C D ... Designation XYZ RTS QWE HGD ... Salary 10000 50000 20000 34343 ... How to do this? 回答1: Basing my answer on a table of the form: CREATE TABLE tbl ( sl_no int , username text , designation text , salary int ); Each row results in a new

How to use table variable in a dynamic sql statement?

。_饼干妹妹 提交于 2019-12-17 02:24:14
问题 In my stored procedure I declared two table variables on top of my procedure. Now I am trying to use that table variable within a dynamic sql statement but I get this error at the time of execution of that procedure. I am using Sql Server 2008. This is how my query looks like, set @col_name = 'Assoc_Item_' + Convert(nvarchar(2), @curr_row1); set @sqlstat = 'update @RelPro set ' + @col_name + ' = (Select relsku From @TSku Where tid = ' + Convert(nvarchar(2), @curr_row1) + ') Where RowID = ' +

error in creating a temp table using dynamic sql

早过忘川 提交于 2019-12-16 18:05:20
问题 declare @TableName nvarchar(max) set @TableName='addresses' DECLARE @sql NVARCHAR(MAX) set @sql= 'create table #tempadd ( ' SELECT @sql=@sql + STUFF( -- Remove first comma ( SELECT ', ' + column_name+' '+ case when DATA_TYPE='varchar' then DATA_TYPE +'(500)' else DATA_TYPE end FROM -- create comma separated values ( SELECT column_name,DATA_TYPE FROM information_schema.columns where table_name = @TableName --Your query here ) AS T FOR XML PATH('') ) ,1,1,'') set @sql =@sql+' ) ' print @sql -

Dynamic query to Union multiple databases

耗尽温柔 提交于 2019-12-14 04:18:31
问题 Say I have the following databases in SQL Server 2008 R2 db1, db2, db3, db4, db5......dbn each database has a table A which contains the columns C1,C2,C3 I can write the following Select statement on two databases to get the data across them: Select C1,C2,C3 FROM db1.dbo.A UNION ALL Select C1,C2,C3 FROM db2.dbo.A However if I have 50 databases on the same server I don't want to write a UNION ALL for each. Can someone give me a script to do this? I can modify the script to exclude system

How to pass OLD, NEW and identifiers to EXECUTE in a trigger function?

﹥>﹥吖頭↗ 提交于 2019-12-14 01:59:15
问题 I'm starting and trying some things in a new database, and am encountering a problem. I'm new in PostgreSQL. I'm trying to create a history for the change of values in a column of a users table. The idea is simple. Whenever there is an update, a new record is inserted in another table (which represents the history). DROP FUNCTION IF EXISTS LOCA_APP.FUNC_HISTORICO_MOD_USUARIOS() CASCADE; CREATE OR REPLACE FUNCTION LOCA_APP.FUNC_HISTORICO_MOD_USUARIOS() RETURNS TRIGGER AS $$ BEGIN EXECUTE

SQL Server : Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '='

僤鯓⒐⒋嵵緔 提交于 2019-12-14 00:57:50
问题 I'm writing a stored procedure and I'm passing the table names as parameters, but I'm having an error in this part: DECLARE @TableA nvarchar(255)='TableA', @DOCID1 nvarchar(MAX), @DOCID2 int; EXEC (' SELECT TOP (1) '+ @DOCID1 +'=DOCID1,'+ @DOCID2 +'=DOCID2 FROM [' + @TABLEA + '] ORDER BY DOCID2') After I run this query I get this error: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '=' I have tried and I can't pinpoint the error, at this point I need some help.. 回答1: I believe you

Pivoting table only returns 1 row

流过昼夜 提交于 2019-12-14 00:13:21
问题 My SQLFiddle: http://sqlfiddle.com/#!2/729a9/1 As you can see, despite there being two rows in the table, there is one row returned. It's also the highest id, so maybe that has something to do with it? I'm stumped like a log, sorry to say. SQL: SELECT GROUP_CONCAT(distinct CONCAT( 'max(case when `pat`.`name` = ''', `pat`.name, ''' then `pa`.`value` end) as `', `pat`.name, '`' ) ) INTO @list FROM `product_attribute_types` pat; SET @sql = CONCAT('select ', @list, ' from `products` `p` LEFT JOIN

Stored procedure is executing with different indexes when called via Entity Framework compared to SSMS

余生长醉 提交于 2019-12-13 17:14:10
问题 We have a stored procedure that executes dynamic sql via sp_executesql . We have observed via the SQL Server profiler and looking at the execution plans the profiler shows that when this procedure is called via SSMS (SQL Server Management Studio) it uses a good combination of indexes, therefore returning in 2 secs. On the other hand, when we call this procedure via our .NET application (called via Entity Framework) - and exactly the same parameters are used compared to the call in SSMS - then